get distinct rows from datatable using Linq (distinct with mulitiple columns)
I am trying to distinct on multiple columns and get datarows from datatable. but getting error.
Dim query As IEnumerable(Of DataRow) =
(From row As DataRow In SourceTable.AsEnumerable() _
Select row.Field(Of String)("ColumnName1"),
row.Field(Of String)("ColumnName2") ).Distinct()
below error:
Unable to cast object of type '<DistinctIt开发者_如何学Goerator>d__7a`1[System.String]'
to type 'System.Collections.Generic.IEnumerable`1[System.Data.DataRow]'.
I want another datatable with distinct row based on given columns from SourceTable.
Try This one then
Dim query = From q In (From p In dt.AsEnumerable() Select New With {.col1= p("ColumnName1"), .col2 = p("ColumnName2")}) Select q.col1, q.col2 Distinct
Try this (a bit of a guess on my part):
Dim query As IEnumerable(Of DataRow) =
(From row As DataRow In SourceTable.AsEnumerable().Distinct() _
Select row.Field(Of String)("ColumnName1"),
row.Field(Of String)("ColumnName2"))
Try this
var distinctRows = (from DataRow dRow in dTable.Rows
select new col1=dRow["dataColumn1"],col2=dRow["dataColumn2"]}).Distinct();
this is in C#. Convert it into vb.net
精彩评论