vb.net下 Linq to dataset 选两列去除重复

lhblxm 2011-11-25 11:57:41
dim query=from dt in tb1 select new with {.name=dt.field(of string)(1),.id=dt.field(of integer)(0)}
dim query1 =query.distinct()
但重复行不能排除
...全文
175 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
q107770540 2011-11-25
  • 打赏
  • 举报
回复
VB的参考这个:


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

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

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

q107770540 2011-11-25
  • 打赏
  • 举报
回复
匿名类里有两个字段,这需要告诉程序:什么情况下的两条数据算相同数据
http://blog.csdn.net/q107770540/article/details/5784646
lhblxm 2011-11-25
  • 打赏
  • 举报
回复
很好用,根据2楼的代码,我的这样写的

dim query=from dt in tb select new Product with {.name=dt.field(of string)(1), _
.Code=dt.field(of integer)}
dim query1=query.distinct(new ProductComparer)


lhblxm 2011-11-25
  • 打赏
  • 举报
回复
十分感谢,我先试试

8,494

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧