Reorder DataTable rows with comma separated values - c#

I have a DataTable dtData
ID name
bs2 bach
js5 hash
lk3 kom
and I have a string IDorder which contains
js5,bs2,lk3
Now I want to reorder my DataTable rows by the order of ID's in IDorder
Expected OutPut
ID name
js5 hash
bs2 bach
lk3 kom
How can I achieve this in C# ?

string IDorder = "js5,bs2,lk3";
DataTable dtData = new DataTable();
//create columns for datatable ID and name
var ordered = dtData.AsEnumerable().OrderBy(x => IDorder.IndexOf(x["ID"]));

Related

Copy Datatable1 values to Datatable2 with the required columns configuration

Copy data from Datatable1 to Datattable2 only for specific columns. I have a method with List of column vlaues and Datatable as input. I need to get all the columns from input datatable and copy to new datatble.
/*Input:
columnNames in list : column1,column2,column3
datatable1 : column1,column2,column5,column6,column3
Output:
datatble2 : column1,column2,column3 (columns from list need to be copied from datatble1 to datatble2 and return datatble2)
*/
public DataTable CopyFromDatatbale(List<string> columnNames,DataTable datatable1)
{
DataTable datatble2=new DataTable();
/*
Code to copy the data from datatable1 to datatble2 with specific columns
*/
}
This is the code i am looking for...Copying data from DataTable1 to DataTable2 for specified columns .Finally in the datatable2 i have all the columns from datatble1 (Only specific columns)
//Copy Columns from Datatable1 to Datatble2 based on columns on columnList
DataView dtView = new DataView(dataTable1);
DataTable dataTable2= new DataTable();
var getColumnNamesCommaSeperated = columnList.Select(x => x.columnNames).ToArray();
dataTable2= dataTable1.Select().CopyToDataTable()
.DefaultView.ToTable(false, getColumnNamesCommaSeperated);

Select distinct DataTable rows

I have a DataTable where sometimes values in all columns in two or more rows repeat. I would like to get distinct DataTable. The solutions from here and here don't work for me because I have many columns and depending on some conditions, the number of columns changes.
I was thinking maybe something like this
System.Data.DataTable table = new System.Data.DataTable(); // already fulfilled table
DataView view = new DataView(table);
var tableDistinct = view.ToTable(true, table.Columns);
But I can't pass table.Columns as an argument.
I don't know what's going wrong because you haven't said what's not working. However, you could use LINQ(-TO-DataTable):
table = table.AsEnumerable()
.GroupBy(r => new{ Col1 = r["Col1"], Col2 = r["Col2"], Col3 = r["Col3"] })
.Select(g => g.First())
.CopyToDataTable();
Change the columns in the anonymous type according to your column-list.
The ToTable access a list of string params, the following should convert all your columns to array of string so you don't have to enter them manually
System.Data.DataTable table = new System.Data.DataTable(); // already fulfilled table
DataView view = new DataView(table);
var tableDistinct = view.ToTable(true, table.Columns.Cast<DataColumn>().Select(z=>z.ColumnName).ToArray());

To display distinct record from datatable on the basis of particular columns but result should be include all columns

i want to get distinct rows from dt on the basis of particulars columns but result should be displayed with all columns.
Here is code this returning all distinct rows but not binding "Quantity" column of default table:
public static DataTable GetDistinctRecords(DataTable dt)
{
DataView view = new DataView(dt);
DataTable distinctValues = dt.DefaultView.ToTable(true, "ContainerCode", "ProductSKU", "CompanyId", "YearId", "MonthId");
return distinctValues;
}
My datatable dt has "ContainerCode", "ProductSKU", "CompanyId", "YearId", "MonthId","Quantity" this columns,i want distict record except using Quatity but my new datatable should display this quantity column.

Creating a datatable with only the rows that match a specific column name prefix using Linq in c#

For example my datatable is like this
A_1 A_2 A_3 ..... A_15 B_1.....B_10 C_1....C_10
x y z........ K
1 2 3.........4
I am trying to create seperate datatables for A,B and C which selects the rows based on column prefix, Also i just need row values in my new datatable.
var query = (from dc in table.Columns.Cast<DataColumn>()
where dc.ColumnName.Contains(prefix)
select table.Rows);
If the above is correct, how to proceed to insert the rows(which is in the query) to the new data table ?
You can create a DataView then copy to a DataTable selecting the columns that match your criteria:
string[] cols = (from dc in table.Columns.Cast<DataColumn>()
where dc.ColumnName.Contains(prefix)
select dc.ColumnName)
.ToArray();
DataView view = new DataView(table);
DataTable selected = view.ToTable(false, cols); // false ==> include "duplicate" rows

Distinct records in DataTable

I want to get distinct records based on some fields. I'm using the following method:
string[] TobeDistinct = { "PKID" };
DataTable dtDistinct = GetDistinctRecords(ds.Tables[0], TobeDistinct);
DataSet ds2 = new System.Data.DataSet();
ds2.Tables.Add(dtDistinct);
public static DataTable GetDistinctRecords(DataTable dt, string[] Columns)
{
DataTable dtUniqRecords = new DataTable();
dtUniqRecords = dt.DefaultView.ToTable(true, Columns);
return dtUniqRecords;
}
This gives me the distinct records, but only two records come. Only two distinct PKID will come. For example, I have multiple records with PKID 10,12,14,16, but the result is 2 rows with PKID 10 and 12. More two rows are not there, but should be there. What do I need to do?
I follow this article: http://www.codeproject.com/Tips/153008/Select-DISTINCT-records-based-on-specified-fields
You can use like follows
DataView view = new DataView(table);
DataTable distinctValues = view.ToTable(true, "Column1", "Column2" ...);
More detail
How to select distinct rows in a datatable and store into an array
Can you try this?
var myResult = dt.AsEnumerable().Select(c => (DataRow)c["MyColumn"]).Distinct().ToList();

Categories