EF Core Query Discover the Basics about Querying a Database in LINQ
Entity Framework Core uses Language Integrate Query (LINQ) to query data from the database.
- LINQ allows you to use C# (or your .NET language of choice) to write strongly typed queries based on your derived context and entity classes.
- Querying in Entity Framework Core remains the same as in EF 6 to load entities from the database.
- It provides more optimized SQL queries and the ability to include C#/VB.NET functions into LINQ-to-Entities queries.
Let's say we have a simple model which contains three entities.
public class Customer { public int CustomerId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Address { get; set; } public virtual List<Invoice> Invoices { get; set; } }
Load All data
The following example load all the data from Customers
table.
using (var context = new MyContext()) { var customers = context.Customers.ToList(); }
Load a Single Entity
The following example load a single record from Customers
table based on CustomerId
.
using (var context = new MyContext()) { var customers = context.Customers .Single(c => c.CustomerId == 1); }
Filter Data
The following example loads all customers with FirstName
Mark.
using (var context = new MyContext()) { var customers = context.Customers .Where(c => c.FirstName == "Mark") .ToList(); }
Author: ZZZ Projects