Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select distinct rows from datatable in Linq

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

Screenshot

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?

like image 735
James123 Avatar asked Jul 14 '10 02:07

James123


People also ask

How to select distinct rows in DataTable in c# using linq?

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

How can I get distinct values from DataTable using Linq in VB net?

You can use ToTable(distinct As Boolean, ParamArray columnNames As String()) method for this. This will return distinct Users for you. You can add multiple column names if you want. Please edit with more information.


1 Answers

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!

like image 200
David Hoerster Avatar answered Oct 02 '22 08:10

David Hoerster