18

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?

Adaline Simonian
  • 4,596
  • 2
  • 24
  • 35
James123
  • 11,184
  • 66
  • 189
  • 343

6 Answers6

45

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
  • 28,421
  • 8
  • 67
  • 102
  • I used attribute1_name there I am getting duplicate records – James123 Jul 14 '10 at 02:56
  • @above You should use "row" during row.Field... I used some other datarow object in from my other loop which yielded me duplicate values.Later I corrected. – Hari Nov 14 '14 at 15:16
  • What if there are 4 columns in a row and you want to distinct based on 2 columns? – Jogi May 10 '16 at 20:47
  • How do I add an `OrderBy` for let's say column `attribute2_name`? – Si8 May 17 '17 at 13:49
5

Like this: (Assuming a typed dataset)

someTable.Select(r => new { r.attribute1_name, r.attribute2_name }).Distinct();
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
4
var Test = (from row in Dataset1.Tables[0].AsEnumerable()
            select row.Field<string>("attribute1_name") + row.Field<int>("attribute2_name")).Distinct();
Adi Lester
  • 24,731
  • 12
  • 95
  • 110
shiju87
  • 112
  • 2
  • 11
1

Check this link

get distinct rows from datatable using Linq (distinct with mulitiple columns)

Or try this

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

EDIT: Placed the missing first curly brace.

Community
  • 1
  • 1
Johnny
  • 1,555
  • 3
  • 14
  • 23
0
Dim distinctValues As List(Of Double) = (From r In _
DirectCast(DataTable.AsEnumerable(),IEnumerable(Of DataRow)) Where (Not r.IsNull("ColName")) _
Select r.Field(Of Double)("ColName")).Distinct().ToList()
Rob
  • 4,927
  • 12
  • 49
  • 54
  • 3
    Welcome to Stack Overflow! Would you consider adding some narrative to explain why this code works, and what makes it an answer to the question? This would be very helpful to the person asking the question, and anyone else who comes along. – Andrew Barber Apr 23 '13 at 18:51
0

We can get the distinct similar to the example shown below

 //example
           var  distinctValues =  DetailedBreakDown_Table.AsEnumerable().Select(r => new
            {
                InvestmentVehicleID = r.Field<string>("InvestmentVehicleID"),
                Universe = r.Field<string>("Universe"),
                AsOfDate = _imqDate,
                Ticker = "",
                Cusip = "",
                PortfolioDate = r.Field<DateTime>("PortfolioDate")

            } ).Distinct();
StackOrder
  • 270
  • 4
  • 14