Enumerable..::.Distinct<(Of <(TSource>)>) 方法 (IEnumerable<(Of <(TSource>)>))
更新:2007 年 11 月
通过使用默认的相等比较器对值进行比较返回序列中的非重复元素。
命名空间: System.Core(在 System.Core.dll 中)
Visual Basic(声明)
<ExtensionAttribute> _ Public Shared Function Distinct(Of TSource) ( _ source As IEnumerable(Of TSource) _ ) As IEnumerable(Of TSource)
Visual Basic (用法)
Of TSource) Dim returnValue As IEnumerable(Of TSource) returnValue = source.Distinct()
C#
static IEnumerable<TSource> Distinct<TSource>(
this IEnumerable<TSource> source
)Visual C++
[ExtensionAttribute] public: generic<typename TSource> static IEnumerable<TSource>^ Distinct( IEnumerable<TSource>^ source )
J#
J# 支持使用泛型 API,但是不支持新泛型 API 的声明。
JScript
JScript 不支持泛型类型或方法。
类型参数
- TSource
-
source 中的元素的类型。
参数
- source
- 类型:
要从中移除重复元素的序列。
使用说明
在 Visual Basic 和 C# 中,可以在 扩展方法(C# 编程指南)。| 异常 | 条件 |
|---|---|
| ArgumentNullException |
source 为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing)。 |
下面的代码示例演示如何使用 Distinct<(Of <(TSource>)>)(IEnumerable<(Of <(TSource>)>)) 返回序列中的非重复元素。
Visual Basic
' Create a list of integers. Dim ages As New List(Of Integer)(New Integer() _ {21, 46, 46, 55, 17, 21, 55, 55}) ' Select the unique numbers in the List. Dim distinctAges As IEnumerable(Of Integer) = ages.Distinct() Dim output As New System.Text.StringBuilder("Distinct ages:" & vbCrLf) For Each age As Integer In distinctAges output.AppendLine(age) Next ' Display the output. MsgBox(output.ToString) ' This code produces the following output: ' ' Distinct ages: ' 21 ' 46 ' 55 ' 17
C#
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
*/