Ricardo Deano
Ricardo Deano

Reputation: 2819

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

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.

Upvotes: 3

Views: 362

Answers (6)

Kef
Kef

Reputation: 1

You can try this.

var unique = list.GroupBy(item => item.Product)
                 .Select(group => new
                                  {
                                      Product = group.Key, 
                                      group.First().ProductName
                                  })
                 .ToList();

Upvotes: 0

Andy Rose
Andy Rose

Reputation: 16984

List<Products> products = GetEverythingService()
 .Select(p => new Products { Product = p.Product, Productname = p.ProductName})
 .Distinct();

As rightly pointed out by dlev in the comments Distinct will only work in this case if the Products class implements the IEqualityComparer<T> interface and overrides the Equals and GetHashCode methods.

This could be overkill if a comparison is only required in this one situation although if product object comparisons are to be carried out elsewhere using the id and product name then it is a viable option. I personally find it a bit more readable than the GroupBy Linq extension but obviously opinions will vary on this.

Upvotes: 0

Henry
Henry

Reputation: 1008

You could try to do it like this.

List<Products> products = new List<Products>();
var listEverything = select d in "data from your ws"
                          select d.Product, d.ProductName).Distinct(x=>x.ProductName);
foreach(var item in listEverything)
{
products.Add(new Products { Product=item.Product, ProductName=item.ProductName});
}

Upvotes: 0

BrokenGlass
BrokenGlass

Reputation: 160952

How about this:

List<Everything> items = ...

var results = items.GroupBy(x => new { x.Product, x.ProductName })
                   .Select(g => new Products() 
                    { 
                       Product = g.Key.Product, 
                       ProductName = g.Key.ProductName 
                    })
                   .ToList();

Upvotes: 0

Jason
Jason

Reputation: 15931

var distinctProducts = everything.Select(e=>new { Product, Productname = e.ProductName }).Distinct();

Upvotes: 0

Aducci
Aducci

Reputation: 26694

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();

Upvotes: 4

Related Questions