views:

55

answers:

3

I am trying to get distinct rows based on multiple columns (attribute1_name, attribute2_name) and get datarows from datatable using Linq-to-Dataset.

alt text

I want results like this

attribute1_name    attribute2_name
--------------     ---------------

Age                State
Age                weekend_percent
Age                statebreaklaw
Age                Annual Sales
Age                Assortment

How to do thin Linq-to-dataset?

A: 

Like this: (Assuming a typed dataset)

someTable.Select(r => new { r.attribute1_name, r.attribute2_name }).Distinct();
SLaks
Don't you still need the call to AsEnumerable()?
Justin Niessner
@Justin: Not for a typed dataset. Tables in typed datasets inherit `TypedTableBase<TRow>`, which implements `IEnumerable<TRow>`.
SLaks
please provide me ... how iterate them
James123
@James: `foreach(var pair in ...)`
SLaks
+1  A: 

If it's not a typed dataset, then you probably want to do something like this, using the Linq-to-DataSet extension methods:

var distinctValues = dsValues.AsEnumerable()
                        .Select(row => new {
                            attribute1_name = row.Field<string>("attribute1_name"),
                            attribute2_name = row.Field<string>("attribute2_name")
                         })
                        .Distinct();

Make sure you have a using System.Data; statement at the beginning of your code in order to enable the Linq-to-Dataset extension methods.

Hope this helps!

David Hoerster
I used attribute1_name there I am getting duplicate records
James123
A: 

Check this link

http://stackoverflow.com/questions/3234341/get-distinct-rows-from-datatable-using-linq-distinct-with-mulitiple-columns/3234374#3234374

Or try this

var distinctRows = (from DataRow dRow in dTable.Rows
                    select new    col1=dRow["dataColumn1"],col2=dRow["dataColumn2"]}).Distinct();
Johnny
You missed a `{`.
SLaks