Solved

Sublist X LINQ

Posted on 2009-07-10
3
684 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 62

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 62

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

13 Experts available now in Live!

Get 1:1 Help Now