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
Related
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?
I would like to do something create dynamic column in Linq for my joined table.
This is my code after asking around. But it give me another error like this.
Code:
var SelectedDT1AndDt2= from dr1 in dt1.AsEnumerable()
join dr2 in dt2.AsEnumerable()
on dr1.Field<Int64>("id1") equals
dr2.Field<Int64>("id2") into joinDt1AndDt2
from leftjoin in joinDt1AndDt2.DefaultIfEmpty()
select dtJoinedTable.LoadDataRow(
(from dc1 in dt1.Columns.Cast<DataColumn>()
select dc1.ColumnName.ToString()).ToArray()
.Union(from dc2 in dt2.Columns.Cast<DataColumn>()
select dc2.ColumnName).ToArray()
, false);
SelectedDT1AndDt2.CopyToDataTable();
Error:
Input string was not in a correct format.Couldn't store in id Column. Expected type is Int64.
This is because the column import is not type Int'64', I check again and confirm that the original col type is Int'32'.
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.
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();
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.