ASP.NET : Filter unique rows from Data table

23,099

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 !

Share:
23,099
Hemant Kothiyal
Author by

Hemant Kothiyal

Trying to find best value of "n" from n number of solutions

Updated on June 20, 2020

Comments

  • Hemant Kothiyal
    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
    Preets almost 15 years
    But of course ! My Google search just happened to land on DevPinoy!
  • Hemant Kothiyal
    Hemant Kothiyal almost 15 years
    Thanks ala, Reply of phoenix and Preets are also great. Using one line statement we can get output.
  • Hemant Kothiyal
    Hemant Kothiyal almost 15 years
    Hi 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