The Entity Framework (EF) is a popular Object-Relational Mapping (ORM) library in the .NET ecosystem, providing a managed, lightweight approach to data access and persistence. When it comes to querying data, the Entity Framework Linq (Language Integrated Query) API shines, allowing C# developers to express queries in a natural, readable manner. Let's explore some real-world examples of Entity Framework Linq queries in C#.

Linq offers an intuitive and expressive way to query data, leveraging the power of C# for complex querying. Whether you're working with simple or complex data models, EF's support for Linq ensures that your data access code is type-safe, maintainable, and efficient.

Basic Linq Queries
Let's start with some basic Linq queries that demonstrate the power and simplicity of Entity Framework Linq.

Retrieving Data
One of the most fundamental operations in data access is retrieving data from the database. With EF Linq, this is as simple as calling the DbSet collection's ToList method, as shown below:

var customers = dbContext.Customers.ToList();
However, to truly harness the power of Linq, let's explore more complex queries.
Filtering Data
Of course, you're usually not going to fetch all data at once. Instead, you'll want to filter your results. Here's how to find all customers from a specific city:

var cityCustomers = dbContext.Customers.Where(c => c.City == "New York").ToList();
Or, to find customers who haven't made a purchase in the last year:
var inactiveCustomers = dbContext.Customers.Where(c => c.LastPurchaseDate < DateTime.Now.AddYears(-1)).ToList();
Advanced Linq Queries
Next, let's delve into some more advanced and complex Entity Framework Linq examples.

Aggregations
EF Linq allows you to perform aggregations using Methoden-Chaining, i.e., chaining method calls to query your data. Here's how you can find the total sales amount by country:








var totalSalesByCountry = dbContext.Orders
.GroupBy(o => o.Customer.Country)
.Select(g => new { Country = g.Key, TotalSales = g.Sum(o => o.Amount) })
.ToList();
And here's how you can find the top 5 best-selling products by quantity:
var topSellingProducts = dbContext.Orders
.GroupBy(o => o.Product)
.Select(g => new { Product = g.Key, QuantitySold = g.Sum(o => o.Quantity) })
.OrderByDescending(p => p.QuantitySold)
.Take(5)
.ToList();
Joins and Subqueries
EF Linq also supports joins and subqueries, allowing you to query data from multiple related entities. Here's how to find customers along with their top 3 best-selling products:
var customersWithTopProducts = dbContext.Customers
.Select(c => new
{
Customer = c,
TopProducts = (
from order in dbContext.Orders
where order.CustomerId == c.Id
group order by order.Product into g
orderby g.Sum(o => o.Quantity) descending
select new
{
Product = g.Key,
TotalQuantity = g.Sum(o => o.Quantity)
}
).Take(3)
})
.ToList();
Exploring these examples, you've seen the power and flexibility that Entity Framework Linq offers to C# developers. Whether you're performing simple or complex queries, EF Linq ensures that your data access code is expressive, efficient, and above all, fun to write.
The possibilities with Entity Framework Linq are endless, and we've barely scratched the surface. So, keep exploring, and enjoy your data access journey with EF. Happy coding!