undef.ms
undef.ms

Reputation: 89

LINQ Select New

I have DataTable that contains duplicate rows. I need take this rows. What I try to do

 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

but how i can to do Select New from this. I need

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

how i can do this in first query

Upvotes: 3

Views: 6245

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460138

You can select an anonymous type

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

Integrated in the query, assuming that the combination of UserId and UserName is unique:

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

Upvotes: 3

Related Questions