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?
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.
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 !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With