Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SelectMany - Simple Linq Question

Posted on 2010-09-22
5
Medium Priority
?
370 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

670 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