Solved

SelectMany - Simple Linq Question

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET 5 Templates 2 78
VB.Net Report Printing Issue 3 34
Help with preventing downloading a zip file 10 37
location of a form 2 14
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 …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 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