c# all distincts value from datatable - c#

I've found this piece of code that can be used to get all distinct values. But my datatable has 10 columns. The distinctValues only shows the columns I write in the toTable(); Is it possible to use this function, but also show the rest of the columns?
DataView view = new DataView(table);
DataTable distinctValues = view.ToTable(true, "Column1", "Column2");

Unless those columns you mention are the full key to the table, there is no guarantee that for a particular combination of those two columns the other columns will have exactly one value.
And if they were the key, then there would be no need to use a "distinct" filter.

You can use Linq-To-DataTable
var distinct = from row in table.AsEnumerable()
group row by new
{
Col1 = row.Field<string>("Column1"),
Col2 = row.Field<string>("Column2")
} into Group
select Group.First()
DataTable tblDistinct = distinctRows.CopyToDataTable();
(assuming that you just want an arbitrary row[the first])

Related

Taking Distinct Column along with other column values also from DataTable in C#

I have a DataTable in which 4 cols are there. Col1, Col2, Col3, Col4.
I want to filter the DataTable to take distinct Col1 values only.
DataView dv = MyDataSet.Tables[0].DefaultView;
listview.ItemsSource = dv.ToTable(true, new string[] { "Col1" }).DefaultView;
when i did this, listview shows only Col1 values but other columns are not showing.
But I cannot do like this,
dv.ToTable(true, new string[] { "Col1", "Col2", "Col3", "Col4" }).DefaultView;
Because i dont want other columns distinct values. More over the Col1 data is duplicating.
Is there any way to do it ? I want to do the filter in MyDataSet.Tables[0] only. Dont want to create another table to store the filter data as I am binding it to a WPF listview.
You need to group by Col1, then take the first row of each group. The easiest way to do it is to use Linq to DataSets:
var query =
from row in MyDataSet.Tables[0].AsEnumerable()
group row by row.Field<int>("Col1") into g
select g.First();
listview.ItemsSource = query.AsDataView();
(replace int with the actual data type of Col1)

How to filter two DataTables in C#?

How to filter two tables in C#? Table one contains full data and table Two contains some content of Table one?
What exactly do you want to do (it's not clear from your question)? Take table1, filter it and then pass the result to table2?
Then:
DataView dv = table1.AsDataView();
dv.RowFilter = fexpression; // for example "MyID = 3"
DataTable table2 = dv.ToTable();
// If you want typed datatable, you can do it like this (although there are other ways):
MyTypedDataTable table2 = new MyTypedDataTable();
DataTable tempTable = dv.ToTable();
table2.Merge(tempTable);

making rows distinct and showing all the columns

In my project there are two datatables dtFail and dtFailed (dtFailed has nothing but column names declarations). dtFail has duplicate "EmployeeName" column values. so i took a dataview dvFail and did the process to make them distinct as shown in the below code:
dtFail
I tried the below code:
DataView dvFail = new DataView(dtFail);
dtFail = dvFail.ToTable(true, "EmployeeName"); //showing only one column in dtFail
dtFailed (only one column)
If i do like below
DataView dvFail = new DataView(dtFail);
dtFail = dvFail.ToTable(true, "EmployeeName","EmployeeRole","Status");
dtFailed (showing but with duplicate rows)
Then the datatable dtFailed is storing duplicate "EmployeeName" also.
Please Help
Thanks in Advance.
Try this query-
DataTable distinctTable = originalTable.DefaultView.ToTable( /*distinct*/ true);
For more info hit below link-
https://social.msdn.microsoft.com/Forums/en-US/ed9c6a6a-a93e-4bf5-a892-d8471b84aa3b/distinct-in-datatable-or-dataview?forum=adodotnetdataset
I hope this would have helped you.
SOLUTION 1:
Based on the question my understanding is, we need to consider duplicates based on EmployeeName and we need not worry about other columns. If that is the case below solution works better.
foreach(DataRow r in dtFail.AsEnumerable())
{
if (!dt1.AsEnumerable().Any(r1 => r1["EmployeeName"] == r["EmployeeName"]))
{
// if you don't want to copy entire row create new DataRow
// with required fields and add that row.
dt1.Rows.Add(r.ItemArray);
}
}
if you want you can put dt1 back to dtFail.
SOLUTION 2:
If we need to consider distinct rows I prefer below solution.
var temp = dtFail.AsEnumerable().Distinct();
dtFail = temp.CopyToDataTable();
I'm not sure it will be helpful or not. As far as I get from your question that you want EmployeeName to be distinct irrelevant to other columns. But if you do ToTable and turn on the distinct flag it will give all the distinct rows, doesn't matter how many columns are involved there. So if you mention only EmployeeName it will obviously give you distinct EmployeeNames, not all the columns associated with it.
So, thats what I did, initially select only the distinct EmployeeName columns and put it into a temp DataTable dtt.
DataTable dtt = dvFail.DefaultView.ToTable(true, "EmployeeName");
Secondly I've created another temp DataTable where we put the segregated rows from the main DataTable dtFail and set the column names manually.
DataTable TempDataTable = new DataTable();
DataTable dtFailed = new DataTable();
Prepare the columns in the dtFailed DataTable.
if (dtFailed.Columns.Count == 0)
{
dtFailed.Columns.Add("EmployeeName");
dtFailed.Columns.Add("EmployeeRole");
dtFailed.Columns.Add("Status");
dtFailed.Columns.Add("Date");
}
Loop through the distinct EmployeeName dtt DataTable and match the EmployeeName and keep that selected first row in the TempDataTable. Finally all rows transferred into the dtFailed.
for (int j = 0; j < dtt.Rows.Count; j++)
{
string EmployeeName = dtt.Rows[j]["EmployeeName"].ToString();
TempDataTable = dvFail.Select("EmployeeName = " + EmployeeName).CopyToDataTable();
dtFailed.Rows.Add(TempDataTable.Rows[0].ItemArray);
}

How to go through two tables to create another table with common values

First Datatable is dt
var dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("First Name");
dt.Rows.Add(1,"name1");
dt.Rows.Add(6,"name6");
dt.Rows.Add(4,"name4");
The second table is dt2
var dt2 = new DataTable();
dt2.Columns.Add("ID");
dt2.Columns.Add("First Name");
dt2.Columns.Add("Last Name");
dt2.Columns.Add("Birthday");
dt2.Rows.Add(1,"name1", "lastName1", 01.01.1991);
dt2.Rows.Add(2,"name2", "lastName2", 02.02.1992);
dt2.Rows.Add(3,"name3", "lastName3", 03.03.1993);
dt2.Rows.Add(4,"name4", "lastName4", 04.04.1994);
dt2.Rows.Add(5,"name5", "lastName5", 05.05.1995);
dt2.Rows.Add(6,"name6", "lastName6", 06.06.1996);
in the third DataTable dt3, I want to get those values ​​where ID is the same
result:
ID Name Birthdate
1 name1 01.01.1991
4 name4 04.04.1994
6 name6 06.06.1996
how to go through the DataTable's in c# ?
Unfortunately, there's no easy way, AFAIK, to join the 2 tables and get the third table automagically unless you are willing to write some code....
You can join them using Linq first:
var common = from c in dt.AsEnumerable()
join x in dt2.AsEnumerable() on c.Field<string>("ID") equals x.Field<string>("ID")
select new object[] { c["ID"],c["First Name"], x["Birthday"] };
And now you can create the destination table with the schema you want:
DataTable dt3 = new DataTable();
dt3.Columns.Add("ID");
dt3.Columns.Add("Name");
dt3.Columns.Add("Birthdate");
foreach (var item in common)
dt3.LoadDataRow(item.ToArray(),true);
Write an SQL Query or a Stored Procedure such that it joins the Two tables like you have depicted. Now use that Query for your DataTable with in .Net. You will get what you need.
Are you adding them to an actual database with a defined schema or just trying to do this in memory? If memory, I would add these to a DataSet which you can then use to filter the criteria becuase in the dataset, you can define their relationship.
You might be looking for adding Relationship between your tables. Check this link.
I guess you will need the DataTables to be in the same DataSet (you can add the DataTables into DataSet).

Selected columns from DataTable

How to get the Selected columns form the DataTable? For e.g my BaseTable has three columns, ColumnA, ColumnB and ColumnC. Now as part of intermediate operations, I need to retrieve all the rows only from the ColumnA. Is there any predefined formula just like DataTable.Select?
DataView.ToTable Method.
DataView view = new DataView(MyDataTable);
DataTable distinctValues = view.ToTable(true, "ColumnA");
Now you can select.
DataRow[] myRows = distinctValues.Select();
From this question: How to select distinct rows in a datatable and store into an array you can get the distinct values:
DataView view = new DataView(table);
DataTable distinctValues = view.ToTable(true, "ColumnA");
If you're dealing with a large DataTable and care about the performance, I would suggest something like the following in .NET 2.0. I'm assuming the type of the data you're displaying is a string so please change as necessary.
Dictionary<string,string> colA = new Dictionary<string,string>();
foreach (DataRow row in table.Rows) {
colA[(string)row["ColumnA"]] = "";
}
return colA.Keys;

Categories

Resources