Solved

Sublist X LINQ

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

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

718 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