0

I have a List of items of type DataItem (List<DataItem>):

public class DataItem
{
    public DataItem() { }

    public string Title { get; set; }

    public string Url { get; set; }

    public string Category { get; set; }
}

There may be many items with the same string in Category field.

How can I extract a list of different possible categories using Linq?

What I want as a result is a List<string> which has all the values found for Category property, but doesn't have repeated values.

anderZubi
  • 6,414
  • 5
  • 37
  • 67
  • 1
    See this similar question: http://stackoverflow.com/questions/10719928/how-to-use-linq-distinct-with-multiple-fields – Oscar Jul 19 '13 at 10:45

2 Answers2

2

You can use the Distinct method:

var result = itemsList.Select(n => n.Category).Distinct().ToList();
It'sNotALie.
  • 22,289
  • 12
  • 68
  • 103
1

yourList.Select(item => item.Category).Distinct().ToList();

Haris Hasan
  • 29,856
  • 10
  • 92
  • 122