简体   繁体   English

如何在 C# 中使用 LINQ 从另一个列表生成唯一的项目列表

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

我有一个类似 {One,Two,Three,One,Four,One,Five,Two,One} 的项目列表,我需要一个查询来获取该列表并仅基于唯一项目生成一个列表,因此该列表返回将是{一,二,三,四,五}。

使用Distinct运算符:

var unique = list.Distinct();

The Distinct operator. Distinct 运算符。 There's an example at MSDN. MSDN 上有一个例子。

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

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

It's worth noting Distinct() will use the default means of determining equality, which might not suit you if your list contains complex objects rather than primitives.值得注意的是 Distinct() 将使用确定相等性的默认方法,如果您的列表包含复杂对象而不是基元,这可能不适合您。

There is an overload that allows you to specify an IEqualityComparer for providing custom equality logic.有一个重载允许您指定一个 IEqualityComparer 以提供自定义相等逻辑。

More details on how Distinct determines if two items are equal: http://msdn.microsoft.com/en-us/library/ms224763.aspx有关 Distinct 如何确定两个项目是否相等的更多详细信息: http : //msdn.microsoft.com/en-us/library/ms224763.aspx

use distinct使用不同的

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

    var rootcategories2 = l.Distinct();

Aside from Distinct, as others have mentioned, you can also use a HashSet:除了 Distinct 之外,正如其他人提到的,您还可以使用 HashSet:

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

If you're using LINQ, though, go with Distinct.但是,如果您使用 LINQ,请使用 Distinct。

Thanks for all your answers, I guess I mis-posed my question slightly.感谢您的所有回答,我想我稍微错误地提出了我的问题。 What I really have is a complex class, on which I want the comparison (for Distinct) based on a particular member of the class.我真正拥有的是一个复杂的类,我想要基于类的特定成员进行比较(对于 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());

I had a similar issue.我有一个类似的问题。 I have a list of importItems with customerName as one of the properties and wanted to generate a list of unique customers from that original list.我有一个 importItems 列表,其中 customerName 作为属性之一,并希望从原始列表中生成唯一客户列表。

I pass the original list to GenCustomers()...using linq I created a unique list of itemCustomers.我将原始列表传递给 GenCustomers()...使用 linq 我创建了一个唯一的 itemCustomers 列表。 I then traverse that list to create a new Customer list.然后我遍历该列表以创建一个新的客户列表。

public static List<Customer> GenCustomers(List<ImportItem> importItems)
{
    List<Customer> customers = new List<Customer>();

    var itemCustomers = importItems.Select(o => new { o.CustomerName }).Distinct();

    foreach (var item in itemCustomers)
    {
        customers.Add(new Customer() { CompanyName = item.CustomerName });
    }

    return customers;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM