Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sublist X LINQ

Posted on 2009-07-10
3
Medium Priority
?
700 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
[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
3 Comments
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 1000 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 64

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 Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

609 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