finding distinct rows in dataset using linq - c#

I am using the below query to find the distinct rows from a dataset but its not getting me the distinct for example its not removing the duplicate and show me the distinct count.
var distinctRows = (from DataRow dRow in _dsMechanic.Tables[0].Rows
select new { col1 = dRow["colName"] }).Distinct();

This should work:
var distinctRows = (
from DataRow dRow in _dsMechanic.Tables[0].Rows
select dRow["colName"]).
Distinct();
Doing the distinct on an anonymous type is just asking for trouble.

Related

Using from DataRow dRow in dt.AsEnumerable() to create a distinct ordered list in C# .NET

I have the following code but am having issues with how to maintain the order:
var cars = (from DataRow dRow in dt.AsEnumerable()
select new
{
Car = dRow["Car"],
CarId = dRow["CarId"],
CarOrder = dRow["CarOrder"]
}).Distinct();
The distinct works well but I need to preserver the CarOrder which goes from 1 to X (ascedning).
The DataTable dt have them all in order correctly but when it hits this distinct code the order does not get preserved.
I am trying to figure out how to use the OrderBy clause.
According to docs
It returns an unordered sequence of the unique items in source
That means that order does not get preserved. So what you can do is probably OrderBy() after the Distinct() as following:
var cars = (from DataRow dRow in dt.AsEnumerable()
select new
{
Car = dRow["Car"],
CarId = dRow["CarId"],
CarOrder = dRow["CarOrder"]
}).Distinct().OrderBy(x => x.CarOrder);
See: Does Distinct() method keep original ordering of sequence intact?

Select multiple rows & colums in datatable where value occurs more than once

I have a table like the picture below:
I am trying to return a result that will return all rows (and columns) where product codes are the same
I have not really used linq before and have playing around with some group by clauses but havent gotten really anywhere, except returning the each individual part code
var GetProductsRows = from DataRow dr in table.Rows
group dr by dr.Field<string>("Product Code") into g
select g;
Somehow I think I am treading water a little out of my depth
A nested linq query should do the trick:
var GetProductsRows = from DataRow dr in table.Rows
group dr by dr.Field<string>("Product Code") into gp
from rows in gp where gp.Count() > 1 select rows;
Basically this will select all rows that belong to groups whose count is greater than one.

DataTable Join using LINQ in C#

I am joining two data tables using LINQ this way:
DataTable targetTable = dataTable1.Clone();
var dt2Columns = dataTable2.Columns.OfType<DataColumn>().Select(dc =>
new DataColumn(dc.ColumnName, dc.DataType, dc.Expression, dc.ColumnMapping));
var dt2FinalColumns = from dc in dt2Columns.AsEnumerable()
where targetTable.Columns.Contains(dc.ColumnName) == false
select dc;
targetTable.Columns.AddRange(dt2FinalColumns.ToArray());
var rowData = from row1 in dataTable1.AsEnumerable()
join row2 in dataTable2.AsEnumerable()
on row1.Field<string>("keyCol") equals row2.Field<string>("keyCol")
select row1.ItemArray.Concat(row2.ItemArray.Where(r2 => row1.ItemArray.Contains(r2) == false)).ToArray();
foreach (object[] values in rowData)
targetTable.Rows.Add(values);
I am facing three issues here:
In case of row count is not same for two tables, I want to pass default value or assign empty string for values not found in other table. How do I achieve this ?
If I have multiple columns and need to compare with AND how is that possible ?
What if I have to join multiple tables in run time. Is there any way to generate dynamic LINQ ?
If both tables have the same primary-key DataTable.Merge will work:
dataTable1.Merge(dataTable2 ,false, MissingSchemaAction.Add);
This will merge the schema(columns) of both tables, joins rows which have the same primary-key and add the other rows.

Select Distinct from DataTable using Linq and C#

I need to select distinct records from a data table using linq and C# and I can't seem to get the syntax correct. I have the following code, which returns all the rows in a data table, how do I just return DISTINCT rows?
DataTable dt = ds.Tables[0];
var q = from dr in dt.AsEnumerable() select dr;
You'll need to use DataRowComparer
IEnumerable<DataRow> distinctRows =
dt.AsEnumerable().Distinct(DataRowComparer.Default);
More info on comparing data rows using linq to dataset can be found here
We could have:
var q = (from dr in dt.AsEnumerable() select dr).Distinct(DataRowComparer.Default);
But really, the from x in ... select x is redundant, so we can have:
var q = dt.AsEnumerable().Distinct(DataRowComparer.Default);
But all AsEnumerable() will do most of the time, is either nothing (if it's already as such) or potentially slow things up (if distinct could be processed better elsewhere), so it's normally better to do:
var q = dt.Distinct(DataRowComparer.Default);
Though there are cases where the former beats the latter.
(from dr in dt.AsEnumerable() select dr).Distinct();

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

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

Categories