Time after time, using Entity Framework Core, we might need to execute raw SQL query to count something in our database (or to use other aggregation function). And while using raw SQL queries to select entities is simple, executing aggregation function is a bit more complicated and needs some “low level” classes, like SqlConnection, SqlCommand, and DbDataReader (class names are a bit different for the different databases, these ones are for the SQL Server).
I wrote small C# extension method for the Entity Framework’s DbContext class to solve this task:
Now you can easily use it like this:
Feel free to use this code and to ask any questions!