Solved

Sublist X LINQ

Posted on 2009-07-10
3
688 Views
Last Modified: 2013-11-11
hello,

I have a class

class Invoice
{
   int id;
   DateTime date;
  List<InvoiceItem> items;
}

class InvoiceItem
{
   Invoice invoice;
   int qty;
   decimal value;
}

if I have
List<Invoice> invoices;
that has a list of my invoices read from DB and their respective invoice items, using LINQ, how to I get a list of invoices and invoice items that has qty > 2?

I tried, without success:
var res =
  from invoice from invoices
 join items in invoice.items on invoices.id equals items.invoice.id
where items.qty > 2
select new { invoice.date, items.value };
0
Comment
Question by:jeovaalmeida
  • 2
3 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 250 total points
ID: 24827762
Hi jeovaalmeida;

The code snippet below should give you what you want.

Fernando
var results = from invoice in invoices
              from item in invoice.items
              where item.qty > 2
              select new { invoice.date, item.value };
 
foreach (var invoice in results)
{
    Console.WriteLine("{0,-24}{1}", invoice.date, invoice.value);
}

Open in new window

0
 

Author Comment

by:jeovaalmeida
ID: 24839275
hello fernando,

thanks for your help. it worked, but ...

why the code didn't return a cartesian result?
the where clause (where item.qty > 2) only filtered qtd > 2.

if I use (where item.qty > 2 && item.invoice.id == invoice.id) (that is, joining the two collections), the result is the same.

is that because invoice.items is a subitem of invoice that the join isn't needed?
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 24839624
Hi jeovaalmeida;

Question:
why the code didn't return a cartesian result?
the where clause (where item.qty > 2) only filtered qtd > 2.

Answer:
It does not return a cartesian product because the query is not doing a join on the class Invoice but is getting the InvoiceItem from the member property of Invoice class which seems to hold the InvoiceItem for each invoice.

Question:
if I use (where item.qty > 2 && item.invoice.id == invoice.id) (that is, joining the two collections), the result is the same.

Answer:
That is NOT joining the two collections. It is ferther filtering the result based on two items in stead of one.

Question:
is that because invoice.items is a subitem of invoice that the join isn't needed?

Answer:
Yes, a join is not needed in this case because InvoiceItem is a collection within the Invoice class.

Fernando
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
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…

697 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