Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SelectMany - Simple Linq Question

Posted on 2010-09-22
5
Medium Priority
?
373 Views
Last Modified: 2013-11-11
I have two lists List<Sales> and List<CashReceived>, the List<CashReceived> has zero-many records for every item in <Sales> (its internet sales, not all payments go thru, sometimes there are multiple attempts hence zero-many). I want a Select which will bring back all the Sales and all of their related CashReceived (or maybe zero ) grouped so I can analyse each group.
Is SelectMany right? I can't see how to make the join outer.
0
Comment
Question by:Silas2
  • 2
  • 2
5 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 33733446
If I understand it correctly, you want all the items from the list<sales> ?

So you need a left-join ?
http://www.developer.com/db/article.php/3739391/Implementing-a-Left-Join-with-LINQ.htm


List<sales> SalesList = ...
List<CashReceived> CashReceivedList = ...

            var LeftJoin = from s in SalesList
                           join c in CashReceivedList on s.SalesID equals c.SalesID into JoinedTables
                           from dept in JoinedTables.DefaultIfEmpty()
                        select new
                        {
                            Id = s.SalesID,
                            ....
                        };

Open in new window

0
 

Author Comment

by:Silas2
ID: 33733995
Right, is it possible to return a collection/list of the many side, i.e. for each item in SalesList have a nested List<CashReceived>?
0
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 500 total points
ID: 33735287
Hi Silas2;

I used method syntac over query syntax because the query will be simpler.

Fernando
var query = salesList.GroupJoin(cashList, s => s.SalesID, c => c.SalesID, (Sale, Cash) => new { Sale, Cash });

foreach (var rec in query)
{
    // Sales object 
    Console.WriteLine("Sales Info = {0} ...", rec.Sale.SalesID);
    // Check to see if there are any CashReceived objects
    if (rec.Cash.Count() == 0) Console.WriteLine("\tNo Cash Received");

    foreach (var recCash in rec.Cash)
    {
        // Iterate through the CashReceived objects
        Console.WriteLine("\tCash Received = {0} ... ", recCash.Amount);
    }
}

Open in new window

0
 

Author Comment

by:Silas2
ID: 33736004
Thanks, that did the trick.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 33739083
Hi Silas2;

You can actually close the question yourself, see help link http://www.experts-exchange.com/help.jsp#hs=29&hi=407 , Select the option "Accept multiple solutions".

Have a great day.
Fernando
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Screencast - Getting to Know the Pipeline

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question