I am trying to get distinct rows based on multiple columns (attribute1_name, attribute2_name) and get datarows from datatable using Linq-to-Dataset.
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?
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 this: (Assuming a typed dataset)
someTable.Select(r => new { r.attribute1_name, r.attribute2_name }).Distinct();
var Test = (from row in Dataset1.Tables[0].AsEnumerable()
select row.Field<string>("attribute1_name") + row.Field<int>("attribute2_name")).Distinct();
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.
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()
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();
来源:https://stackoverflow.com/questions/3242892/select-distinct-rows-from-datatable-in-linq