How to generate a unique list of items from another list using LINQ in C#

Asked
Viewd1388

4

I have a list of items like so {One,Two,Three,One,Four,One,Five,Two,One} and I need a query that takes that list and generates a list based on only unique items, so the list returned would be {One,Two,Three,Four,Five}.

1388

7 个答案

3
         var x = new string[] {"One", "Two", "Three", "One", "Four", "One", "Five", "Two", "One"}.ToList();
        var distinct = x.Distinct();
 
1

除了Distinct,正如其他人提到的那样,您还可以使用HashSet:

 List<string> distinct = new HashSet<string>(list).ToList();
 

但是,如果您使用的是LINQ,请选择Distinct。

0

感谢您的所有回答,我想我对问题的提出有误。我真正拥有的是一个复杂的类,我希望基于该类的特定成员进行比较(对于Distinct)。

 class ComplexClass
{
    public string Name{ get; set; }
    public string DisplayName{ get; set; }
    public int ComplexID{ get; set; }
}

List<ComplexClass> complexClassList = new List<ComplexClass>();

complexClassList.Add(new ComplexClass(){Name="1", DisplayName="One", ComplexID=1});
complexClassList.Add(new ComplexClass(){Name="2", DisplayName="Two", ComplexID=2});
complexClassList.Add(new ComplexClass(){Name="3", DisplayName="One", ComplexID=1});

// This doesn't produce a distinct list, since the comparison is Default
List<ComplexClass) uniqueList = complexClassList.Distinct();

class ComplexClassNameComparer : IEquatable<ComplexClass>
{
    public override bool Equals(ComplexClass x, ComplexClass y)
    {
        return (x.To.DisplayName == y.To.DisplayName);
    }

    public override int GetHashCode(ComplexClass obj)
    {
        return obj.DisplayName.GetHashCode();
    }
}

// This does produce a distinct list, since the comparison is specific
List<ComplexClass) uniqueList = Enumerable.Distinct(complexClassList , new ComplexClassNameComparer());
 
  • Thanks Kirk, however, in this case, I felt it more accurate to answer instead of editing the question and invalidating all the answers.

    October 05, 2009 11:14
  • Thanks to those who already pointed that out, and for the suggestions of HashSets. Using Distinct with a custom IEqualityComparer comparer worked.

    October 05, 2009 07:21
  • Your ComplexClassComparer has a discrepancy between the implementations of Equals and GetHashCode. Make sure the returned values are always consistent, which isn’t obvious at the moment. GetHashCode should rather return obj.To.DisplayName.GetHashCode().

    Konrad RudolphOctober 05, 2009 07:25
  • You should edit your original question rather than post an answer to it. SO will prompt you to do this.

    Kirk BroadhurstOctober 05, 2009 07:23
  • Thanks Konrad, answer has been edited. My bad, copy/paste error snuck in.

    October 05, 2009 12:22
1

使用与众不同

     List<string> l = new List<string>
    {
        "One","Two","Three","One","Four","One","Five","Two","One"
    };

    var rootcategories2 = l.Distinct();
 
17

使用不同运算符:

 var unique = list.Distinct();
 
2

值得注意的是,Distinct()将使用默认的确定相等性的方法,如果您的列表包含复杂的对象而不是基元,则可能不适合您。

有一个重载,允许您指定IEqualityComparer来提供自定义相等逻辑。

有关Distinct如何确定两个项目是否相等的更多详细信息: http:/ /msdn.microsoft.com/en-us/library/ms224763.aspx

5

唯一运算符。 MSDN中有一个示例。

http://msdn.microsoft.com/en-us/library /bb348436.aspx