Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq & C# - Inserting distinct data from one class into another

Tags:

c#

linq

Afternoon all

I have a lovely webservice call that brings back a list, we'll call it List<Everything>

This would return something along the lines of:

Product ProductName SomethingElse
1           Dave    abc
1           Dave    def
1           Dave    ghi
2           Jason   abc
2           Jason   def
3           Terry   abc
3           Terry   def
3           Terry   ghi
3           Terry   jkl

I then have another List<Products> (int Product, string ProductName) that I would like to populate using the distinct product information in List<Everything>.

So I'm trying to get the following result:

Product Productname
1       Dave
2       Jason
3       Terry

How can I achieve this using Linq?

Apologies for what is probably bloody obvious.

like image 292
Ricardo Deano Avatar asked Mar 10 '26 01:03

Ricardo Deano


1 Answers

List<Products> products = (from x in everythingList
                          group x by new { x.Product, x.ProductName } into xg
                          select new Products
                          {
                             Product = xg.Key.Product,
                             ProductName = xg.Key.ProductName 
                          }).ToList();
like image 76
Aducci Avatar answered Mar 11 '26 15:03

Aducci



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!