In this topic, you will learn how to perform basic query operations against the domain model by using Language Integrated Query (LINQ):
Loading Full Entities
The following example shows how to execute a LINQ query that returns a collection of instances of a domain model type.
| C# |
Copy Code |
|
using ( EntitiesModel dbContext = new EntitiesModel() ) { IEnumerable<Category> categories = dbContext.Categories.ToList(); } |
| VB.NET |
Copy Code |
|
Using dbContext As New EntitiesModel() Dim categories As IEnumerable(Of Category) = dbContext.Categories.ToList() End Using |
Filtering Data
The following example shows you how to filter which records to be returned from the database.
| C# |
Copy Code |
|
using ( EntitiesModel dbContext = new EntitiesModel() ) { IEnumerable<Category> categories = dbContext.Categories.Where( c => c.CategoryName == "SUV" ); } |
| VB.NET |
Copy Code |
|
Using dbContext As New EntitiesModel() Dim categories As IEnumerable(Of Category) = dbContext. Categories.Where(Function(c) c.CategoryName = "SUV") End Using |
Ordering Data
The following example shows a query that, when executed, retrieves all Car objects. An SQL ORDER BY clause orders the returned objects by Model and Make.
| C# |
Copy Code |
|
using ( EntitiesModel dbContext = new EntitiesModel() ) { List<Car> cars = ( from car in dbContext.Cars orderby car.Model, car.Make select car ).ToList(); } |
| VB.NET |
Copy Code |
|
Using dbContext As New EntitiesModel() Dim cars As List(Of Car) = ( From car In dbContext.Cars Order By car.Model, car.Make Select car).ToList() End Using |
For further reference: