How to Merge rows data From DataTable in C#? - c#

How to Merge rows data From DataTable in C#?

I think that this is a good use case for the LINQ Group clause. You can start with something like this:
var rowGroups = dataTable.Rows.GroupBy(row =>
new {RecptNo = row["recpt_no"], Test = row["Test"]});
foreach(var group in rowGroups)
{
//Here "group" is a collection of rows with the same rectp_no and test. Process as required.
//You could also check group.Key.RecptNo and group.Key.Test here if necessary.
}

Here's a link of that relates to your question. Hope it helps.
How to merge rows in a DataTable when data in multiple columns match?

Related

subtract two DataTables that have the same shema, E.g Minus SQL query

I have two DataTables that have both the same schema (same columns names and types ).I want to get the rows that appear in the first and not in the second one.
Could anyone help me in this? Thanks.
Thankfully, MS has created extension methods for DataTable that let you use Linq methods (like Except) to query data rows, and written a class that implements IEqualityComparer<DataRow> that compares DataRow instances by their column values:
var rows = dt1.AsEnumerable()
.Except(dt2.AsEnumerable(),DataRowComparer.Default);
I would write something like this:
var onlyIn1 = dataTable1.Where( row1 => !dataTable2.Rows.Any( row2 => row1["CustomerKey"] == row2["CustomerKey"]) ).ToList();
You could try this:
var onlyIn1 = db.Table1.Except(db.Table2).ToList();
I never test this, but it should work like T-SQL's EXCEPT

How to get unique records of specific columns of data table

I have a DataTable imported from Excel file.
Data i need is only unique from specific columns of the DataTable.
The unique data i meant is like when a command DISTINCT is used in SQL Select Query.
I want to get the list of the unique data from the DataTable Column and put them into List
I think LinQ can be used for this matter but i'm not so familiar with it.
I was thinking of code like this below
var data is from MyDataTable
where MyDataTable.ColumnName = "SpecificColumn"
select MyDataTable["SpecificColumn"]).UniqueData;
List<string> MyUniqueData = new List<string>();
foreach(object obj in data)
{
if(MyUniqueData.NotContain(obj))
MyUniqueData.add(obj);
}
I hope someone can drop off some knowledge to me.
var unique = data.Distinct().ToList();
What you're looking for is .Distinct(). See MSDN documentation here. You can specify your own comparer if you need something specific and it will return only unique records.
If you have a Datatable or DataView, inorder to get unique records from a column, you have to write this.
this would be simple.
DataTable dtNew = dt.DefaultView.ToTable(true, "ColName"); // for Datatable
DataTable dtnew= dv.ToTable(true, "ColName"); // for DataView

Filter DataTable Efficiently

What's the most efficient way of filtering DataRows in a DataTable? I have a list of integers and want to retrieve all rows (and eventually create a DataTable from them) which match the integers in the list. I'm currently using the code below, but it's quite slow. Am I missing a more efficient way?
foreach (var i in integerlist)
{
DataRow dr = (from row in originalDataTable.AsEnumerable()
where row.Field<int>("urlID") == i
select row).FirstOrDefault<DataRow>();
if (dr!= null)
{
newDataTable.Rows.Add(dr);
}
}
I suggest you to try to do vice versa.
foreach (var row in originalDataTable)
{
if(integerList.Contains( (int)row["urlID"]))
newDataTable.ImportRow(row)
}
It makes even more sense if you have more rows in dataset then integers in your int collection.
Hope it helps :)
Hm... may be I'm missing something, but...
Woudn't be it easier just use DataView and apply a RowFilter for it ?
you could try doing a join such as:
var resultSet =
from row in originalDataTable.AsEnumerable()
join i in integerlist
on row.Field<int>("urlID") equals i
select row;
that should give you the full result set.
if you need a datatable you could do:
resultSet.CopyToDataTable();
As #Tigran says you can use the dataview, check this msdn article on how to accomplish just that.
Basically you use a DataView to filter the data and the you call the DataView.ToTable method to get the new DataTable.

How to remove more DataTable Columns using C#.Net?

I have One DataTable may have more Columns. But "NetAmount", "TotalAmount", "Destination" are the DataTable Columns which always present in the DataTable.
Here I want to Remove the three Columns such as "NetAmount", "TotalAmount" and "Destination" from the DataTable and to take the other column values in the DataTable.
I tried like the below and get the Desired Output.
dtAttribute.Columns.Remove("NetAmount"); //dtAttribute is the Main DataTable
dtAttribute.Columns.Remove("TotalAmount");
dtAttribute.Columns.Remove("Destination");
DataTable dtItem = dtAttribute.Copy();
But it looks like very childish and lengthy. Is there any other method to do? Please give suggestions.
There's nothing wrong with your code (except that you are copying the table after removing the columns -- are you sure that this is what you want?).
If you want something more abstract (instead of repeating the same line again and again), you might consider removing the columns in a loop:
var dtItem = dtAttribute.Copy(); // if you want to keep a copy of the original table
var toRemove = new string[] {"NetAmount", "TotalAmount", "Destination"};
foreach (col in toRemove)
dtItem.Columns.Remove(col);
Instead of removing the columns, how about not putting them in the DataTable in the first place?
First select columns which you want to remove, then remove them
List<string> toRemove = dt.Columns.Cast<DataColumn>().Where(c => c.ColumnName.StartsWith("ExtraColumn")).Select(c => c.ColumnName).ToList();
foreach (var col in toRemove) dt.Columns.Remove(col);

Getting duplicates count for each distinct value from a datatable

I've a datatable which has a single text column 'Title' which can have multiple values with duplicates. I can remove the duplicates using a dataview.
DataView v = new DataView(tempTable);
tempTable = v.ToTable(true, "Title");
But how can i get the number of duplicates for each distinct value without any looping?
If you don't want to loop or use Linq, so there is no way to do that but you can use a computed column on the data table with one more condition if applicable with you. That is the data should be in two related tables like this.
DataRelation rel = new DataRelation("CustToOrders", data.Tables["Customers"].Columns["customerid"], data.Tables["Orders"].Columns["customerid"]);
data.Relations.Add(rel);
Given that customerid field as a Foreign key in the Orders table so it has duplicates.
You can get the count of the duplicates this way:
data.Tables["Customers"].Columns.Add("Duplicates",
GetType(Decimal), "Count(child.customerid)");
The way I would get the results that you want would look something like this:
tempTable.Rows.Cast<DataRow>()
.Select(dr => Convert.ToString(dr[0]))
.GroupBy(dr => dr)
.Select(g => new { Title = g.Key, Count = g.Count() });
However, it's actually looping under the hood. In fact, I can't think of a way to do that kind of a grouping without inspecting each record.
The drawback is that the result of that expression is a sequence of anonymous type instances. If you still want the result to be a DataView, you could rewrite the last Select to create a new DataRow with two columns, and shove them into a new DataTable which you pass to the DataView.

Categories