1

I have a detailed list and I want a new one with the elements of one property with no duplicates like this.

List<Class1> list = List<Class1>();
list.Add(new Class1("1", "Walmart", 13.54));
list.Add(new Class1("2", "Target", 12.54));
list.Add(new Class1("3", "Walmart", 14.54));
list.Add(new Class1("4", "BestBuy", 16.54));
list.Add(new Class1("5", "Walmart", 19.54));
list.Add(new Class1("6", "Amazon", 12.33));

My new list

List<Stores> newList = list.Select / FindAll / Group ?

I want this collection

newList = "Walmart", "Target", "BestBuy", "Amazon"
Maximus Decimus
  • 4,901
  • 22
  • 67
  • 95
  • possible duplicate of [Get one of each](http://stackoverflow.com/questions/18083392/get-one-of-each) – I4V Aug 07 '13 at 20:10

4 Answers4

3

You need Distinct and Select.

var newList = list.Select(x => x.Name).Distinct().ToList();

If you also want your original class, you would have to get a bit more fancy.

Either get MoreLINQ and use its DistinctBy method:

var newList = list.DistinctBy(x => x.Name).ToList();

Or use a clever GroupBy hack:

var newList = list.GroupBy(x => x.Name).Select(x => x.First());
It'sNotALie.
  • 22,289
  • 12
  • 68
  • 103
3

Lets assume that Class1 is defined as :

public class Class1
{
    string Id {get;set;}
    string Store {get;set;}
    double Price {get;set;}
}

You can have your result as:

var result = list.Select(x => x.Store).Distinct().ToList();
Giannis Paraskevopoulos
  • 18,261
  • 1
  • 49
  • 69
2

You want to use Select() to select a specific property of the items:

list.Select(c => c.Company);

This returns an IEnumerable<string>.

You would then want to call .Distinct().

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0
newList = list.Select(x => x.Store).Distinct().ToList();
Kevin DeVoe
  • 600
  • 2
  • 8
  • Huh. This seems like an exact copy of another answer. – It'sNotALie. Aug 07 '13 at 20:09
  • @It'sNotALie. All 4 answers have that same exact code snippet; it just that simple of a problem with that one obvious answer. – Servy Aug 07 '13 at 20:17
  • @Servy Sure, but I wasn't expecting the exact same lambda parameter name **and** name of the field to distinct on. – It'sNotALie. Aug 07 '13 at 20:18
  • @It'sNotALie. three out of four have the same parameter name, and the list name is not sufficiently distinctive that it would surprise me for two people to come up with it on their own. – Servy Aug 07 '13 at 20:21
  • @It'sNotALie. Oh, and it's also the name of the list used in the question itself to hold the results, so my guess if they both copied it from there. – Servy Aug 07 '13 at 20:23
  • @Servy Oh, I'm talking about `x` and `Store`. Not about `newList`. – It'sNotALie. Aug 07 '13 at 20:23
  • @It'sNotALie. Then it's not a complete copy of any answer. The only other answer that uses `Store` doesn't use `newList`. None of the code samples are identical with respect to *all* invented variable/parameter names. – Servy Aug 07 '13 at 20:25
  • @It'sNotALie. x is pretty standard usage in a linq statement. newList and list were what was used in the question, and store is being used because the list is a list of Stores and store is the singular form of that. Is it really that surprising that multiple people have a similar answer when most people follow the same set of standards and conventions? – Kevin DeVoe Aug 08 '13 at 13:36