ASP.NET:从数据表中过滤唯一行

发布于 2024-08-01 18:44:43 字数 577 浏览 5 评论 0 原文

您好,我正在 asp.net 中使用数据表。 我无法了解如何从数据表中过滤唯一数据。

问题如下所述,

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

我想根据第一列和第二列应用的过滤器进行以下输出。 在输出中我们可以看到第一列和第二列有独特的组合。

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

如何在数据表上应用过滤器?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

方觉久 2024-08-08 18:44:44
yourdataset.Tables["TableName"].DefaultView.ToTable(true,"disticecolumn");

创建并返回一个新的数据表
基于现有的行
数据视图。

.ToTable 方法中的 true 指定返回的 DataTable 包含其所有列都具有不同值的行。

来自 msdn 文章

编辑:

从数据库中执行此操作会更容易,您可以使用“distinct”关键字。

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.

一个人的夜不怕黑 2024-08-08 18:44:44
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/存档/2005/02/10/370548.aspx

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]];
}

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

厌味 2024-08-08 18:44:44

显然,您无法在 DataTable 的 Select() 方法上应用 DISTINCT 过滤器。

但是,您可以使用以下代码行创建一个新的 DataTable:

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

这应该返回满足您要求的不同行!

归功于 DevPinoy.org

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 !

Hello爱情风 2024-08-08 18:44:44

您可以使用 LINQ 执行以下操作:

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

请参阅帖子 使用 LINQ to操作DataTable的数据

最佳Rgds

You can use LINQ to do something like this :

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

see the post Using LINQ to manipulate DataTable's data

best Rgds

牵强ㄟ 2024-08-08 18:44:44
DataTable filteredTable = yourDataTable.DefaultView.ToTable("TargetTable", true, "Consumer", "No", "Date");

这段代码满足了我的要求...谢谢! 很多!

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

this code fulfilled my requirement....Thanks! A lot!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文