I have a table in my SQL Anywhere 12.0.1 database:
CREATE TABLE Entries (
ListId UNIQUEIDENTIFIER NOT NULL,
. . .
);
I need to query the table to get the number of rows in the table that have the same ListID. In SQL:
SELECT ListID, COUNT(*)
FROM Entries
GROUP BY ListId;
I want to execute this query using Entity Framework 4.x, and I want to return the results as a Dictionary<Guid, long>. Something like this:
public Dictionary<Guid, long> GetRowCounts( MyEntities context ) {
Dictionary<Guid, long> result = null;
try {
result = ( from entry in Entries
group entry by entry.ListId into listGroup
select listGroup ).ToDictionary( grp => grp.Key, ???? );
} catch ( Exception ex ) {
. . .
}
return result;
}
Remember, I want the count of rows that have each unique ListId returned. What do I put in place of the “????”?
Well, look at what you wrote:
(emphasis mine). So you need:
Personally I’d write the method like this though:
… or if you still want it to be a
Dictionary<Guid, long>, useg => g.LongCount(), or just cast:You almost certainly don’t want the try/catch block there, and returning directly is a lot simpler. Likewise, unless you’re writing a moderately complex query, there’s really no benefit in using a query expression. Learn to use the extension methods directly too, so that for each query you can write the simplest possible code.