Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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#?

like image 875
ANP Avatar asked Jul 29 '10 14:07

ANP


People also ask

How do you find the number of rows in a dataset?

Count the number of rows and columns of Dataframe using len() function. The len() function returns the length rows of the Dataframe, we can filter a number of columns using the df. columns to get the count of columns.

How are the number of rows calculated?

ROWS is useful if we wish to find out the number of rows in a range. The most basic formula used is =ROWS(rng). The function counted the number of rows and returned a numerical value as the result. When we gave the cell reference B6, it returned the result of 1 as only one reference was given.


2 Answers

Try this:

dsDataSet.Tables[0].Select("COMPLEATED_ON is not null").Length;
like image 130
rosscj2533 Avatar answered Sep 28 '22 07:09

rosscj2533


You can use the Select method of the DataTable:

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++;
   }
}
like image 22
djdd87 Avatar answered Sep 28 '22 07:09

djdd87