Enumerable.Distinct 方法

定义

返回序列中的非重复元素。

重载

Distinct<TSource>(IEnumerable<TSource>, IEqualityComparer<TSource>)

通过使用指定的 IEqualityComparer<T> 对值进行比较,返回序列中的非重复元素。

Distinct<TSource>(IEnumerable<TSource>)

通过使用默认的相等比较器对值进行比较,返回序列中的非重复元素。

注解

结果序列是无序的。

Distinct<TSource>(IEnumerable<TSource>, IEqualityComparer<TSource>)

Source:
Distinct.cs
Source:
Distinct.cs
Source:
Distinct.cs

通过使用指定的 IEqualityComparer<T> 对值进行比较,返回序列中的非重复元素。

C#
public static System.Collections.Generic.IEnumerable<TSource> Distinct<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, System.Collections.Generic.IEqualityComparer<TSource> comparer);
C#
public static System.Collections.Generic.IEnumerable<TSource> Distinct<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, System.Collections.Generic.IEqualityComparer<TSource>? comparer);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要从中移除重复元素的序列。

comparer
IEqualityComparer<TSource>

用于比较值的 IEqualityComparer<T>

返回

IEnumerable<TSource>

一个包含源序列中的非重复元素的 IEnumerable<T>

例外

sourcenull

示例

以下示例演示如何实现可在 方法中使用的 Distinct 相等比较器。

C#
public class Product
{
    public string Name { get; set; }
    public int Code { get; set; }
}

// Custom comparer for the Product class
class ProductComparer : IEqualityComparer<Product>
{
    // Products are equal if their names and product numbers are equal.
    public bool Equals(Product x, Product y)
    {

        //Check whether the compared objects reference the same data.
        if (Object.ReferenceEquals(x, y)) return true;

        //Check whether any of the compared objects is null.
        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        //Check whether the products' properties are equal.
        return x.Code == y.Code && x.Name == y.Name;
    }

    // If Equals() returns true for a pair of objects
    // then GetHashCode() must return the same value for these objects.

    public int GetHashCode(Product product)
    {
        //Check whether the object is null
        if (Object.ReferenceEquals(product, null)) return 0;

        //Get hash code for the Name field if it is not null.
        int hashProductName = product.Name == null ? 0 : product.Name.GetHashCode();

        //Get hash code for the Code field.
        int hashProductCode = product.Code.GetHashCode();

        //Calculate the hash code for the product.
        return hashProductName ^ hashProductCode;
    }
}

实现此比较器后,可以在 方法中使用Distinct对象的序列Product,如以下示例所示:

C#
Product[] products = { new Product { Name = "apple", Code = 9 },
                       new Product { Name = "orange", Code = 4 },
                       new Product { Name = "apple", Code = 9 },
                       new Product { Name = "lemon", Code = 12 } };

// Exclude duplicates.

IEnumerable<Product> noduplicates =
    products.Distinct(new ProductComparer());

foreach (var product in noduplicates)
    Console.WriteLine(product.Name + " " + product.Code);

/*
    This code produces the following output:
    apple 9
    orange 4
    lemon 12
*/

注解

此方法通过使用延迟执行来实现。 即时返回值是一个对象,用于存储执行操作所需的所有信息。 在通过直接调用GetEnumerator其方法或通过在 C# For Eachforeach Visual Basic 中使用 来枚举对象之前,不会执行此方法表示的查询。

方法 Distinct<TSource>(IEnumerable<TSource>, IEqualityComparer<TSource>) 返回不包含重复值的无序序列。 如果 comparernull,则使用默认相等比较器 Default来比较值。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Distinct<TSource>(IEnumerable<TSource>)

Source:
Distinct.cs
Source:
Distinct.cs
Source:
Distinct.cs

通过使用默认的相等比较器对值进行比较,返回序列中的非重复元素。

C#
public static System.Collections.Generic.IEnumerable<TSource> Distinct<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要从中移除重复元素的序列。

返回

IEnumerable<TSource>

一个包含源序列中的非重复元素的 IEnumerable<T>

例外

sourcenull

示例

下面的代码示例演示如何使用 Distinct<TSource>(IEnumerable<TSource>) 从整数序列返回不同的元素。

C#
List<int> ages = new List<int> { 21, 46, 46, 55, 17, 21, 55, 55 };

IEnumerable<int> distinctAges = ages.Distinct();

Console.WriteLine("Distinct ages:");

foreach (int age in distinctAges)
{
    Console.WriteLine(age);
}

/*
 This code produces the following output:

 Distinct ages:
 21
 46
 55
 17
*/

如果要从某些自定义数据类型的对象序列中返回不同的元素,则必须在 类中实现 IEquatable<T> 泛型接口。 下面的代码示例演示如何在自定义数据类型中实现此接口,并提供 GetHashCodeEquals 方法。

C#
public class MyProduct : IEquatable<MyProduct>
{
    public string Name { get; set; }
    public int Code { get; set; }

    public bool Equals(MyProduct other)
    {
        //Check whether the compared object is null.
        if (Object.ReferenceEquals(other, null)) return false;

        //Check whether the compared object references the same data.
        if (Object.ReferenceEquals(this, other)) return true;

        //Check whether the products' properties are equal.
        return Code.Equals(other.Code) && Name.Equals(other.Name);
    }

    // If Equals() returns true for a pair of objects
    // then GetHashCode() must return the same value for these objects.

    public override int GetHashCode()
    {

        //Get hash code for the Name field if it is not null.
        int hashProductName = Name == null ? 0 : Name.GetHashCode();

        //Get hash code for the Code field.
        int hashProductCode = Code.GetHashCode();

        //Calculate the hash code for the product.
        return hashProductName ^ hashProductCode;
    }
}

实现此接口后,可以在 方法中使用Distinct<TSource>(IEnumerable<TSource>)对象的序列Product,如以下示例所示:

C#
MyProduct[] products = { new MyProduct { Name = "apple", Code = 9 },
                       new MyProduct { Name = "orange", Code = 4 },
                       new MyProduct { Name = "apple", Code = 9 },
                       new MyProduct { Name = "lemon", Code = 12 } };

// Exclude duplicates.

IEnumerable<MyProduct> noduplicates =
    products.Distinct();

foreach (var product in noduplicates)
    Console.WriteLine(product.Name + " " + product.Code);

/*
    This code produces the following output:
    apple 9
    orange 4
    lemon 12
*/

注解

此方法通过使用延迟执行来实现。 即时返回值是一个对象,用于存储执行操作所需的所有信息。 在通过直接调用GetEnumerator其方法或通过在 C# For Eachforeach Visual Basic 中使用 来枚举对象之前,不会执行此方法表示的查询。

方法 Distinct<TSource>(IEnumerable<TSource>) 返回不包含重复值的无序序列。 它使用默认相等比较器 Default来比较值。

在 Visual Basic 查询表达式语法中, Distinct 子句转换为 的 Distinct调用。

默认相等比较器 Default用于比较实现 IEquatable<T> 泛型接口的类型的值。 若要比较自定义数据类型,需要实现此接口并为该类型提供自己的 GetHashCodeEquals 方法。

有关使用 IEqualityComparer<T> 定义自定义比较器的示例,请参阅 Distinct<TSource>(IEnumerable<TSource>, IEqualityComparer<TSource>)

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0