ASP.NET : Filter unique rows from Data table
Solution 1
yourdataset.Tables["TableName"].DefaultView.ToTable(true,"disticecolumn");
Creates and returns a new DataTable based on rows in an existing DataView.
true in the .ToTable method specifies that returned DataTable contains rows that have distinct values for all its columns.
From msdn article
Edit:
It would be easier to do this from a database where you can use the 'distinct' keyword.
Solution 2
There is apparently no way you can apply a DISTINCT filter on the Select() method of the DataTable.
However, you can create a new DataTable using the following line of code:
DataTable filterTable = yourDataTable.DefaultView.ToTable("TargetTable", true, "Consumer", "No", "Date");
This should return distinct rows, that meet your requirement!
Credit to DevPinoy.org !
Hemant Kothiyal
Trying to find best value of "n" from n number of solutions
Updated on June 20, 2020Comments
-
Hemant Kothiyal almost 4 years
Hi i am using data table in asp.net. I am not able to get how can we filter unique data from data table .
Problem is describe below
Data Table: Consumer No Date Value ABC001 1st Aug 09 1 ABC001 1st Aug 09 2 ABC001 2nd Aug 09 1 XYZ001 1st Aug 09 1 XYZ002 1st Aug 09 1 XYZ002 1st Aug 09 2
I would like following output based upon filter applied over first and second column. In output we can see that there is unique combination of first and second column.
Consumer No Date ABC001 1st Aug 09 ABC001 2nd Aug 09 XYZ001 1st Aug 09 XYZ002 1st Aug 09
How can i apply filter on data table?
-
Preets almost 15 yearsBut of course ! My Google search just happened to land on DevPinoy!
-
Hemant Kothiyal almost 15 yearsThanks ala, Reply of phoenix and Preets are also great. Using one line statement we can get output.
-
Hemant Kothiyal almost 15 yearsHi my thanks to you both Phoenix and Preets. Also my credit goes to both of you because you become path for my destination. Thanks again