Find count of certain elements in the dataset

Govind Sankar

Active member
Joined
May 15, 2020
Messages
27
Programming Experience
Beginner
Hi, I have a database and a dataset in C# application. The dataset has the list of my friends with phone number, address and a id. This is for learning purposes. I need to find the count of certain elements , say for example the total count of all the friends with ID > 3. How can I do that?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,575
Location
Virginia Beach, VA
Programming Experience
10+
Based on our OP's original set of threads, more likely than not these are typed datasets.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,049
Location
Sydney, Australia
Programming Experience
10+
Assuming a typed DataSet, there are three ways to do this:
  1. Call Fill or GetData on your table adapter and then use a LINQ query on the DataTable to get a record count using a predicate.
  2. Add a query to your table adapter in the designer with an appropriate filter, then call the corresponding FillBy or GetDataBy method on your table adapter and use the return value of the FillBy method or get a record count from the DataTable.
  3. Add a scalar query to your DataSet in the designer, which will create a new table adapter, then call the corresponding method on that at run time.
The first option is the least efficient as it gets all the data when you only want to count some of it, but requires no changes to the DataSet. The second option is more efficient because it only gets the data you want to count, but it still gets the data that you don't need to use, but it only requires a change to an existing table adapter. The third option is the most efficient because it only returns the record count and not the records, but requires addition of a new table adapter.
 
Top Bottom