Solved

SelectMany - Simple Linq Question

Posted on 2010-09-22
5
364 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 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 63

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 63

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 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 …
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

636 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