Saturday, August 24, 2019

Entity Framework Core GROUP BY (Grouping)

In the previous posts we saw on how to get data from a SQL Server table using EF Core context, how to filter data using various types of filters and how to Sort the data using sorting keys. In this post we shall see on how to perform Aggregate operations like COUNT, MAX, MIN etc by Grouping rows based on keys. This is similar to the GROUP BY clause in SQL Server.

Let’s start with a GROUP BY query which will get the count of employees in each of the City in the Employee table.

Entity Framework Core ORDER BY (Sorting)

In this post we shall see on how to sort the results from a table using LINQ Query, this is similar to the ORDER BY clause in SQL Server. In EF Core LINQ queries we can achieve this by using OrderBy.

Let us get the list of Employees in the Employee table sorted by the Name of the employee, this can be done using the following LINQ query.

Friday, August 23, 2019

Entity Framework Core sub-query (Filter rows based on results from another table)

In the previous post we saw on how to implement different types of row filters using LINQ queries, like Contains, StartsWith, EndsWith etc. In this post we shall see a more complex type of filter where the rows from the Employee table are filtered based on a specific type of Department Name.

We want to get the list of employees who belong to a Department whose name contains the word Account. In SQL server we can do this using a sub-query as follows.

Entity Framework Core Where clause (Filter rows based on conditions)

To filter a table for specific rows we need to use Where clause and specify a filter condition in the Where clause.

To get all employees who have Age > 30 we need to use the following LINQ Query