How to: Query Data
In this topic, you will learn how to perform basic query operations against the 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 model type.
using ( FluentModel dbContext = new FluentModel() )
{
IEnumerable<Category> categories = dbContext.Categories.ToList();
}
Using dbContext As New FluentModel()
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.
using ( FluentModel dbContext = new FluentModel() )
{
IEnumerable<Category> categories = dbContext.Categories.Where(
c => c.CategoryName == "SUV" );
}
Using dbContext As New FluentModel()
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.
using ( FluentModel dbContext = new FluentModel() )
{
List<Car> cars = ( from car in dbContext.Cars
orderby car.Model, car.Make
select car ).ToList();
}
Using dbContext As New FluentModel()
Dim cars As List(Of Car) = (
From car In dbContext.Cars
Order By car.Model, car.Make
Select car).ToList()
End Using