ASP.NET: Filter unique rows from a data table

Hi, I am using a data table in asp.net. I cannot get how we can filter unique data from a data table.

The problem is described 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 the following result to be based on a filter applied to the first and second columns. In conclusion, we see that there is a unique combination of the first and second columns.

Consumer No Date             
ABC001           1st Aug 09 
ABC001           2nd Aug 09 
XYZ001           1st Aug 09 
XYZ002           1st Aug 09

How can I apply a filter in a data table?

+3
source share
5 answers
yourdataset.Tables["TableName"].DefaultView.ToTable(true,"disticecolumn");

Creates and returns a new DataTable based on the rows in an existing DataView.

true in the .ToTable method indicates that the returned DataTable contains rows that have different values ​​for all its columns.

msdn

Edit:

, .

+10

-, DISTINCT Select() DataTable.

DataTable, :

DataTable filterTable = yourDataTable.DefaultView.ToTable("TargetTable", true, "Consumer", "No", "Date");

, !

DevPinoy.org!

+3
private static DataTable SelectDistinct(DataTable SourceTable, params string[] FieldNames)
{
     object[] lastValues;
     DataTable newTable;
     DataRow[] orderedRows;

     if (FieldNames == null || FieldNames.Length == 0)
          throw new ArgumentNullException("FieldNames");

     lastValues = new object[FieldNames.Length];
     newTable = new DataTable();

     foreach (string fieldName in FieldNames)
          newTable.Columns.Add(fieldName, SourceTable.Columns[fieldName].DataType);

     orderedRows = SourceTable.Select("", string.Join(", ", FieldNames));

     foreach (DataRow row in orderedRows)
     {
          if (!fieldValuesAreEqual(lastValues, row, FieldNames))
          {
               newTable.Rows.Add(createRowClone(row, newTable.NewRow(), FieldNames));

               setLastValues(lastValues, row, FieldNames);
          }
     }

     return newTable;
}

private static bool fieldValuesAreEqual(object[] lastValues, DataRow currentRow, string[] fieldNames)
{
     bool areEqual = true;

     for (int i = 0; i < fieldNames.Length; i++)
     {
          if (lastValues[i] == null || !lastValues[i].Equals(currentRow[fieldNames[i]]))
          {
               areEqual = false;
               break;
          }
     }

     return areEqual;
}

private static DataRow createRowClone(DataRow sourceRow, DataRow newRow, string[] fieldNames)
{
     foreach (string field in fieldNames)
          newRow[field] = sourceRow[field];

     return newRow;
}

private static void setLastValues(object[] lastValues, DataRow sourceRow, string[] fieldNames)
{
     for (int i = 0; i < fieldNames.Length; i++)
          lastValues[i] = sourceRow[fieldNames[i]];
}

: http://weblogs.asp.net/eporter/archive/2005/02/10/370548.aspx

0

LINQ, - :

var resultSet = (from r in dt.AsEnumerable() 
    select r["ConsumerNo"]).Distinct().ToList(); 

. LINQ DataTable

Rgds

0
DataTable filteredTable = yourDataTable.DefaultView.ToTable("TargetTable", true, "Consumer", "No", "Date");

this code fulfilled my requirement .... Thank you! Lot!

0
source

Source: https://habr.com/ru/post/1715143/


All Articles