【问题标题】:ASP.NET : Filter unique rows from Data tableASP.NET:过滤数据表中的唯一行
【发布时间】:2015-06-20 01:41:02
【问题描述】:

您好,我正在使用 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

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

【问题讨论】:

  • 你不能循环遍历它,删除重复项吗?
  • 嗨,不需要循环。请阅读 phoenix/Preets 的回复。它对我有帮助

标签: c# c#-2.0


【解决方案1】:
yourdataset.Tables["TableName"].DefaultView.ToTable(true,"disticecolumn");

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

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

来自msdn的文章

编辑:

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

【讨论】:

    【解决方案2】:

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

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

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

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

    感谢DevPinoy.org

    【讨论】:

    • 当然!我的 Google 搜索恰好登陆 DevPinoy!
    • 您好,感谢 Phoenix 和 Preets。我的功劳也归功于你们两个,因为你们成为了我到达目的地的道路。再次感谢
    【解决方案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

    【讨论】:

    • 谢谢ala,phoenix和Preets的回复也很棒。使用一行语句,我们可以得到输出。
    【解决方案4】:

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

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

    见帖子Using LINQ to manipulate DataTable's data

    最佳 Rgds

    【讨论】:

      【解决方案5】:
      DataTable filteredTable = yourDataTable.DefaultView.ToTable("TargetTable", true, "Consumer", "No", "Date");
      

      此代码满足我的要求....谢谢!很多!

      【讨论】:

        猜你喜欢
        • 2017-07-30
        • 1970-01-01
        • 2021-01-04
        • 2018-03-04
        • 2021-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多