Solved

SelectMany - Simple Linq Question

Posted on 2010-09-22
5
342 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 125 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 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 125 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 62

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now