【问题标题】:Calculate number of rows from dataset从数据集中计算行数
【发布时间】:2010-07-29 22:53:20
【问题描述】:

在我后面的代码中,我在数据集中放置了一个查询结果,它返回如下表:

Sl_NO    COMPLEATED_ON      SCORE      IS_PRESENT      MAX_VALUE
1         29/07/2010          4            0              12
2         29/07/2010          5            0              13
3         29/07/2010          6            1              23
4         29/07/2010          7            1              44
5                             6            1
6                             5            0
7                             4            1

我的要求是我需要计算列名为 COMPLEATED_ON 的非空行总数。我的意思是从上面的示例中它应该返回 4,因为 COMPLEATED_ON 列的其余三行是空的。谁能告诉我该怎么做这在 C# 中?

【问题讨论】:

    标签: c# asp.net dataset


    【解决方案1】:

    试试这个:

    dsDataSet.Tables[0].Select("COMPLEATED_ON is not null").Length;
    

    【讨论】:

      【解决方案2】:

      可以使用DataTable的Select方法:

      DataTable table = DataSet.Tables[tableName];
      
      string expression = "COMPLEATED_ON IS NOT NULL AND COMPLEATED_ON <> ''";
      DataRow[] foundRows = table.Select(expression);
      
      int rowCount = foundRows.Length;
      

      或者这是一种蛮力方式:

      int count = 0;
      
      DataTable table = DataSet.Tables[tableName];
      foreach (DataRow row in table.Rows)
      {
         string completed = (string)row["COMPLEATED_ON"];
         if (!string.IsNullOrEmpty(completed))
         {
            count++;
         }
      }
      

      【讨论】:

        【解决方案3】:

        您可以使用 LINQ 来确定:

        var nonEmptyRows = (from DataRow record in myDataSet.Tables[0].AsEnumerable()
                           where !string.IsNullOrEmpty(record.Field<string>("COMPLEATED_ON"))
                           select record).Count();
        

        nonEmptyRows 将成为 int,为您提供非空行数。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-12-10
          • 2021-07-02
          • 1970-01-01
          • 1970-01-01
          • 2016-10-06
          • 2012-08-28
          • 1970-01-01
          • 2016-07-02
          相关资源
          最近更新 更多