Count of Records
-
I have a 20 records in a database table. Each record has and ID. I want to read a field in each record fieldA. I only want to read the records that have an ID '1'. For each record that has an ID '1' and fieldA Is Not Null I want to return a count of those records. So if 10 records have data in fieldA and have ID '1' then my count for fieldA will be 10. Anyone know how to do this in Linq?
-
I have a 20 records in a database table. Each record has and ID. I want to read a field in each record fieldA. I only want to read the records that have an ID '1'. For each record that has an ID '1' and fieldA Is Not Null I want to return a count of those records. So if 10 records have data in fieldA and have ID '1' then my count for fieldA will be 10. Anyone know how to do this in Linq?
You have already posted this in the C# forum[^], and received a perfectly decent answer.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
I have a 20 records in a database table. Each record has and ID. I want to read a field in each record fieldA. I only want to read the records that have an ID '1'. For each record that has an ID '1' and fieldA Is Not Null I want to return a count of those records. So if 10 records have data in fieldA and have ID '1' then my count for fieldA will be 10. Anyone know how to do this in Linq?
It's possible to achieve that using Linq To Dataset[^]. Assuming that you did Load[^] data into
DataTable
object via using SqlReader[^]:var result = dt.AsEnumerable()
.GroupBy(a=>a.Field("ID"))
.Select(g=>new
{
Id = g.Key,
Count = g.Count()
})
.ToList();Returns a list of
Id
's with the number of occurences. For further information, please see: Programming Guide (LINQ to DataSet)[^] Queries in LINQ to DataSet[^] Querying DataSets (LINQ to DataSet)[^] LINQ to DataSet Examples[^]