Answered by:
Count items in DataTable/DataView

Question
-
User-1826049516 posted
Hey,
Is there a better of doing this (the 4 integers):
try { using (Connection = new SqlConnection(BasePage.SqlConnection)) using (Command = new SqlCommand("genSelUsers", Connection)) { Command.CommandType = CommandType.StoredProcedure; Command.Parameters.AddWithValue("@AbsenceOnly", "false"); using (SqlDataAdapter da = new SqlDataAdapter(Command)) { ds = new DataSet(); da.Fill(ds); ds.Tables[0].TableName = "Users"; ds.Tables[1].TableName = "Absences"; ds.Tables[2].TableName = "Clockings"; ds.Relations.Add("Users_Absences", ds.Tables["Users"].Columns["User_Number"], ds.Tables["Absences"].Columns["EMP_Number"], false); // ds.Relations[0].Nested = true; ds.Relations.Add("Users_Clockings", ds.Tables["Users"].Columns["User_Number"], ds.Tables["Clockings"].Columns["EMP_Number"], false); // ds.Relations[1].Nested = true; } } int StaffTotal = ds.Tables["Users"].Select().Length; int StaffIn = ds.Tables["Users"].Select("status = 'in'").Length; int StaffOut = ds.Tables["Users"].Select("status = 'out'").Length; int StaffFilter = ds.Tables["Users"].Select(BasePage.SearchExpression(SearchValue, "Staff")).Length; DataView Staff_View = ds.Tables["Users"].DefaultView; Staff_View.RowFilter = BasePage.SearchExpression(SearchValue, "Staff"); Staff_View.Sort = SortExpression; Staff_Repeater.DataSource = Staff_View; Staff_Repeater.DataBind(); Literal StaffTotal_Literal = (Literal)Staff_Repeater.Controls[0].FindControl("StaffTotal_Literal"); StaffTotal_Literal.Text = StaffTotal.ToString(); Literal StaffIn_Literal = (Literal)Staff_Repeater.Controls[0].FindControl("StaffIn_Literal"); StaffIn_Literal.Text = StaffIn.ToString(); Literal StaffOut_Literal = (Literal)Staff_Repeater.Controls[0].FindControl("StaffOut_Literal"); StaffOut_Literal.Text = StaffOut.ToString(); Literal StaffFilter_Literal = (Literal)Staff_Repeater.Controls[0].FindControl("StaffFilter_Literal"); if (String.IsNullOrEmpty(BasePage.SearchExpression(SearchValue, "Staff"))) { StaffFilter_Literal.Text = "not applied"; } else { StaffFilter_Literal.Text = StaffFilter.ToString(); }
Thanks
Thursday, September 1, 2016 5:26 PM
Answers
-
User765422875 posted
What you are doing is OK. You can also use Linq.
As an example:
int StaffIn = ds.Tables["Users"].AsEnumerable().Count(row => row.Field<string>("status") == "in");
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, September 1, 2016 6:30 PM -
User-707554951 posted
Hi ldoodle,
From your description, are you want to count number of items in data view or data table, if that the case, I suggest you could use count method to get items’s number in data view or in datatable,
You could do as below:int StaffTotal = ds.Tables["Users"].Select().Count(); int StaffIn = ds.Tables["Users"].Select("status = 'in'").Count(); int StaffOut = ds.Tables["Users"].Select("status = 'out'").Count(); int StaffFilter = ds.Tables["Users"].Select(BasePage.SearchExpression(SearchValue, "Staff")).Count();
Hope this can help you. If you have any question and confusion about the problem. Please don't hesitate to let me know.
Best regards
Cathy- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, September 2, 2016 5:28 AM
All replies
-
User765422875 posted
What you are doing is OK. You can also use Linq.
As an example:
int StaffIn = ds.Tables["Users"].AsEnumerable().Count(row => row.Field<string>("status") == "in");
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, September 1, 2016 6:30 PM -
User-707554951 posted
Hi ldoodle,
From your description, are you want to count number of items in data view or data table, if that the case, I suggest you could use count method to get items’s number in data view or in datatable,
You could do as below:int StaffTotal = ds.Tables["Users"].Select().Count(); int StaffIn = ds.Tables["Users"].Select("status = 'in'").Count(); int StaffOut = ds.Tables["Users"].Select("status = 'out'").Count(); int StaffFilter = ds.Tables["Users"].Select(BasePage.SearchExpression(SearchValue, "Staff")).Count();
Hope this can help you. If you have any question and confusion about the problem. Please don't hesitate to let me know.
Best regards
Cathy- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, September 2, 2016 5:28 AM -
User-1826049516 posted
Thanks both. Good to know the other ways.
Friday, September 2, 2016 11:08 AM