When we think about querying databases, the first thing that pops to mind is usually some SQL query. Then other questions arise in regards to the database type, connection, query design, etc.

LINQ to Entities combined with Entity Framework allows the developers to skip a big portion of these questions and worries.

Entity Framework handles the database structure and connection while LINQ to Entities gives the possibility to write database queries in the same way as for any other data collection operation with LINQ.

LINQ to Entities Construction and Execution

In Entity Framework, the collections of entities are represented by the DbSet class which is derived from IQueriable. That means that we can use LINQ queries to query DbSet.

LINQ to Entities converts the LINQ queries to the command tree queries. Command tree is a query representation that the Entity Framework can understand.

Then the command tree queries get converted to the SQL queries and executed against the target database. The result-set is converted back to the list of appropriate entity objects.

The processes of creation and execution of LINQ to Entities queries takes five steps:

  1. Construct the ObjectQuery instance from ObjectContext.
  2. Compose the LINQ to Entities query using ObjectQuery instance.
  3. Convert the LINQ query to the command tree query.
  4. Execute the command tree query against the data source.
  5. Retrieve the query results back to the client.

#c# #programming #linq

Introducing LINQ to Entities
1.70 GEEK