Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET : Filter unique rows from Data table

Tags:

c#

c#-2.0

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?

like image 591
Hemant Kothiyal Avatar asked Aug 03 '26 14:08

Hemant Kothiyal


2 Answers

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.

like image 172
rahul Avatar answered Aug 06 '26 03:08

rahul


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 !

like image 41
Preets Avatar answered Aug 06 '26 04:08

Preets