简体   繁体   English

计算数据集中的行数

[英]Calculate number of rows from dataset

In my code behind I have put an query result in dataset which returns a table like as bellow: 在我的代码后面,我在数据集中放了一个查询结果,返回一个像下面这样的表:

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

My reqirement is that I need to count total number non empty rows which column name is COMPLEATED_ON.I mean from above example it should return 4 since rest three rows of the column COMPLEATED_ON is empty.Can any one tell me how to do this in C#? 我的要求是我需要计算列数名为COMPLEATED_ON的非空行总数。我的意思是从上面的例子它应该返回4,因为其余三行的列COMPLEATED_ON是空的。任何人都可以告诉我如何在C#中执行此操作?

试试这个:

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

You can use the Select method of the DataTable: 您可以使用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;

Or here's a brute force way: 或者这是一种蛮力的方式:

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

You could use LINQ to determine this: 您可以使用LINQ来确定:

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

nonEmptyRows would then be an int giving you the count of non-empty rows. 然后, nonEmptyRows将是一个int为您提供非空行的计数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM