473,503 Members | 1,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

LINQ query from a list<> inside of a list<>

5 New Member
Hey everyone,

I've dealt with some simple LINQ queries before, but am not sure how to tackle this one...

I've got a project I'm working on where I'm dealing with two classes I've created, 'Package' and 'Product' (along with other classes).

I've also created a Job class which houses a List<Package> and inside the Package class it houses a List<Product>.

(NOTE): a Package is just a grouping of Products. so each Package could have 2 different scenarios: 1)it has all unique Products or 2) it could have some of the same Products that other Packages have and some unique)

Expand|Select|Wrap|Line Numbers
  1. //This is just an abstract view of the classes
  2. //    there are other things in each class
  3.  
  4. public class Job
  5. {
  6.     int jobID;
  7.     List<Package> packages;
  8. }
  9.  
  10. public class Package
  11. {
  12.     int packageID;
  13.     List<Product> products;
  14. }
  15.  
  16. public class Product
  17. {
  18.     int productID;
  19.     int quantity;
  20. }
  21.  


So now for my question, as I explained in the above note, some Packages can have the same Product in them. Well I'm looking to write a function in the Job class to pull a LINQ query to return each unique Product class, AND also the sum of the 'quantity' data member in the Product class.

This analogy may help: if a grocery store wanted to take every purchase (package class) from a single day (job class) and wanted to know the total quantity of each product sold and the name of the product.

the ideal query would return:
(ID#1:Qty 4),(ID#2:Qty 1),(ID#3:Qty 2),(ID#12:Qty 3),(ID#13:Qty 1)



If I did a horrible job of explaining or if you need more info, just let me know and I'll find another way of explaining it!

Thank you!
Dec 21 '10 #1
1 2798
Bryan Cheung
55 New Member
You'll have to play with the Distinct() and Sum() methods from List<>.

GL,
Bryan.
Dec 22 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1709
by: vishal | last post by:
i am making an application. the user can give description and that description is stored in database and then the description is shown on html page. suppose the user has written "<b>hello</b>"...
2
2374
by: Alexander Malkis | last post by:
//Consider: class A { /*...*/ }; template<class T> class list {/*... */ }; void f(const list<const A*> lst) { /*...doesn't change the arg...*/ } void g(list<A*> lst) { f(lst);...
2
2562
by: Andrew Donaldson | last post by:
I'd welcome some help in understanding what's going on with graphical browsers in the navigation list at: http://www.bounceandtickle.org.uk/index.html (This site is not about what it might...
2
4496
by: Lucian Wischik | last post by:
Does ReadOnlyCollection<T> really implement IList<T>, like it claims to do? ... When I right-click on ReadOnlyCollection and look at its definition, it says this: public class...
4
17105
by: bob_jeffcoat | last post by:
Hi, Have class B which subclasses from class A. I have a List<B> and I'm trying to get a List<A> without writing a stupid loop everytime I want to do it. So I tried this method: public...
9
6969
by: Stephan Steiner | last post by:
Hi I seem to have a bit of trouble understanding one bit of how generics work: In C#, every class automatically derives from object, and inherits a bunch of properties (i.e. ToString()). Thus,...
2
6789
by: Oleg Subachev | last post by:
What is the difference between Collection<Tand List<T? Both of them implement the same set of interfaces. Oleg Subachev
10
4996
by: arnuld | last post by:
It is quite an ugly hack but it is all I am able to come up with for now :-( and it does the requires work. I want to improve the program, I know you people have much better ideas ;-) /* C++...
1
8861
by: shapper | last post by:
Hello, On my MVC projects I often create classes which contains properties which are lists of other classes. Should I start using IQueryable<Tor IEnumerable<Tinstead of List<T>? What are...
6
1957
by: gsBytes | last post by:
I am working with a specialized graph class, which has a node class and an edge class. In the node class, there is a list of pointers to each edge. Code that uses the graph must be able to see what...
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7087
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5579
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
383
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.