5

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 '<DistinctIterator>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.

1

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

How to user 'query' objects. I mean How to iterate that?
0

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")) 

2 Comments

Ah shit I should have paid more attention to the error message. I've got it now. Your problem isn't your SQL syntax - it's the datatype you expect since you're marking it as an Enumerable collection of DataRows when you're projecting tuples which contain a pair of strings.
Change the syntax so you're projecting a new DataRow with each of the strings added as columns - either that or strip the As IEnumerable(Of DataRow) and just accept the implict type as a result.
0

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

2 Comments

You're missing a curly brace at the front of your LINQ projection, but I get your meaning.
This one is not returing distinct columns

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.