本文整理匯總了VB.NET中System.Linq.Enumerable.Distinct方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET Enumerable.Distinct方法的具體用法?VB.NET Enumerable.Distinct怎麽用?VB.NET Enumerable.Distinct使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。
在下文中一共展示了Enumerable.Distinct方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: List
' 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.
Console.WriteLine(output.ToString)
輸出:
Distinct ages: 21 46 55 17
示例2: IEquatable
Public Class Product
Implements IEquatable(Of Product)
Public Property Name As String
Public Property Code As Integer
Public Function Equals1(
ByVal other As Product
) As Boolean Implements IEquatable(Of Product).Equals
' Check whether the compared object is null.
If other Is Nothing Then Return False
' Check whether the compared object references the same data.
If Me Is Other Then Return True
' Check whether the products' properties are equal.
Return Code.Equals(other.Code) AndAlso Name.Equals(other.Name)
End Function
Public Overrides Function GetHashCode() As Integer
' Get hash code for the Name field if it is not null.
Dim hashProductName = If(Name Is Nothing, 0, Name.GetHashCode())
' Get hash code for the Code field.
Dim hashProductCode = Code.GetHashCode()
' Calculate the hash code for the product.
Return hashProductName Xor hashProductCode
End Function
End Class
示例3: products
Dim products() As Product =
{New Product With {.Name = "apple", .Code = 9},
New Product With {.Name = "orange", .Code = 4},
New Product With {.Name = "apple", .Code = 9},
New Product With {.Name = "lemon", .Code = 12}}
' Exclude duplicates.
Dim noduplicates = products.Distinct()
For Each product In noduplicates
Console.WriteLine(product.Name & " " & product.Code)
Next
輸出:
apple 9 orange 4 lemon 12
示例4: IEqualityComparer
Public Class Product
Public Property Name As String
Public Property Code As Integer
End Class
' Custom comparer for the Product class
Public Class ProductComparer
Implements IEqualityComparer(Of Product)
Public Function Equals1(
ByVal x As Product,
ByVal y As Product
) As Boolean Implements IEqualityComparer(Of Product).Equals
' Check whether the compared objects reference the same data.
If x Is y Then Return True
'Check whether any of the compared objects is null.
If x Is Nothing OrElse y Is Nothing Then Return False
' Check whether the products' properties are equal.
Return (x.Code = y.Code) AndAlso (x.Name = y.Name)
End Function
Public Function GetHashCode1(
ByVal product As Product
) As Integer Implements IEqualityComparer(Of Product).GetHashCode
' Check whether the object is null.
If product Is Nothing Then Return 0
' Get hash code for the Name field if it is not null.
Dim hashProductName =
If(product.Name Is Nothing, 0, product.Name.GetHashCode())
' Get hash code for the Code field.
Dim hashProductCode = product.Code.GetHashCode()
' Calculate the hash code for the product.
Return hashProductName Xor hashProductCode
End Function
End Class
示例5: products
Dim products() As Product =
{New Product With {.Name = "apple", .Code = 9},
New Product With {.Name = "orange", .Code = 4},
New Product With {.Name = "apple", .Code = 9},
New Product With {.Name = "lemon", .Code = 12}}
' Exclude duplicates.
Dim noduplicates = products.Distinct(New ProductComparer())
For Each product In noduplicates
Console.WriteLine(product.Name & " " & product.Code)
Next
輸出:
apple 9 orange 4 lemon 12