robnhood00
asked on
Remove Duplicate Rows in datatable using LINQ
I have a datatable that gets it's results from a stored proc that I cannot modity.
The datatable returns the following:
Name Note
John Sales Rep
John Sales Rep
Sarah Administrative
Liz Customer Service
Liz Customer Service
I want to use LINQ to remove the dupes before binding it to a GridView. Please help me solve this using LINQ only.
Here's what I have but it's returning the same result set as above:
da = New SqlDataAdapter(sSQL, c)
da.Fill(dt)
query = (From h In dt.AsEnumerable() _
Select New With { _
.Name= h.Field(Of String)("Name"), _
.Note = h.Field(Of String)("Note") _
}).ToList().Distinct()
gv.DataSource = query
gv.DataBind()
The datatable returns the following:
Name Note
John Sales Rep
John Sales Rep
Sarah Administrative
Liz Customer Service
Liz Customer Service
I want to use LINQ to remove the dupes before binding it to a GridView. Please help me solve this using LINQ only.
Here's what I have but it's returning the same result set as above:
da = New SqlDataAdapter(sSQL, c)
da.Fill(dt)
query = (From h In dt.AsEnumerable() _
Select New With { _
.Name= h.Field(Of String)("Name"), _
.Note = h.Field(Of String)("Note") _
}).ToList().Distinct()
gv.DataSource = query
gv.DataBind()
what is dt?
This is one of those that you have to implement an IEquatable class (I think its called)
Public Class ItemsToGrid
Implements IEquatable(Of ItemsToGrid)
Public Property Name As String
Public Property Note As Integer
Public Function Equals1(ByVal other As ItemsToGrid) As Boolean Implements IEquatable(Of ItemsToGrid).Equals
If other Is Nothing Then Return False
If Me Is other Then Return True
Return Note.Equals(other.Note) AndAlso Name.Equals(other.Name)
End Function
Public Overrides Function GetHashCode() As Integer
Dim hashProductName = If(Name Is Nothing, 0, Name.GetHashCode())
Dim hashProductCode = Note.GetHashCode()
Return hashProductName Xor hashProductCode
End Function
End Class
then use your code with a New ItemsToGrid in the line Select New ItemsToGrid With ...query = (From h In dt.AsEnumerable() _
Select New ItemsToGrid With { _
.Name = h.Field(Of String)("Name"), _
.Note = h.Field(Of String)("Note") _
}).ToList().Distinct()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Refer : Anonymous Types in Visual Basic
would Option.Infer = On in the project compile properties solve the Anonymous types isue then?
and it would still have to be a Select New with else it will error
query = (From h In dt.AsEnumerable() _
Select New ItemsToGrid With { _
.Name = h.Field(Of String)("Name"), _
.Note = h.Field(Of String)("Note") _
} Distinct).ToList()
try this and reply if having an any problem
Regards,
nishantcomp2512
Regards,
nishantcomp2512
da = New SqlDataAdapter(sSQL, c)
da.Fill(dt)
var query = dt.Rows.Distinct().select(X=>X) ;
gv.DataSource = query
gv.DataBind();
ASKER
Thanks for all the quick comments. I tried this first and it worked. Thanks!