LINQ选择新建

时间:2014-03-17 10:38:33

标签: vb.net linq

我有包含重复行的DataTable。 我需要拿这行。 我尝试做什么

 Dim dups1 = From row In objDataSet.Tables(0).AsEnumerable() _
                       Let UserId = row.Field(Of Integer)("UserId") _
                       Group row By UserId Into grp = Group _
                       Where (grp.Count() = 2) _
                       Select grp

但我该如何做从这里选择新的。我需要

Select New UserName = row("UserName"), UserId = row("UserId")

我如何在第一次查询中执行此操作

1 个答案:

答案 0 :(得分:3)

您可以选择匿名类型

Select New With { .UserName = row.Field(Of String)("Username"), .UserId = row.Field(Of Integer)("UserId") }

集成在查询中,假设UserIdUserName的组合是唯一的:

Dim dups1 = From row In objDataSet.Tables(0).AsEnumerable()
            Group row By UserCols = New With {
                Key .UserId = row.Field(Of Integer)("UserId"),
                Key .UserName = row.Field(Of String)("Username")
            } Into Group
            Where Group.Count() > 1
            Select UserCols