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
Solved

Sublist X LINQ

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 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