If I have a table with a title column and 3 bit columns (f1, f2, f3) that contain either 1 or NULL, how would I write the LINQ to return the title with the count of each bit column that contains 1? I’m looking for the equivalent of this SQL query:
SELECT title, COUNT(f1), COUNT(f2), COUNT(f3) FROM myTable GROUP BY title
I’m looking for the ‘best’ way to do it. The version I came up with dips into the table 4 times when you look at the underlying SQL, so it’s too slow.
Here’s the solution I came up with. Note that it’s close to the solution proposed by @OdeToCode (but in VB syntax), with one major difference:
The first query does the grouping, but the ToList gets the grouped data as-is from the server. Eliminating the counting here keeps the resulting SQL statement from producing sub-SELECTs for each count. I do the counting in the second query locally.
This works since I know the first query will return a manageable number of rows. If it were returning millions of rows, I’d probably have to go in another direction.