Executing Entity Framework SQL Queries

Using the Entity Framework, when you execute a query, let's say 2,000 records that require group and some other calculations, does the query on the server execute only the results sent to the client, or are they all sent to the client and then executed?

This is the use of SQL Server.

I am studying this because I am going to start a project in which there will be a huge number of queries required for a huge database, and I want to know if this will lead to a significant network load if you use the Entity Framework.

+4
source share
4 answers

I would think that all database queries are executed on the server side (where the database is!), And the results are transmitted. However, in Linq, you have something called Delayed Execution (lazily loaded), so your information is not actually retrieved until you try to access it, for example. calling ToList () or accessing a property (linked table).

You have the option of using LoadWith to load if you need it.

So, in terms of performance, if you really want to make 1 trip to the database for your query (which has related tables), I would recommend using the LoadWith options. However, this does depend on the specific situation.

+3
source

It always runs on SQL Server. It also means that sometimes you need to change this:

 from q in ctx.Bar where q.Id == new Guid(someString) select q 

to

 Guid g = new Guid(someString); from q in ctx.Bar where q.Id == g select q 

This is because the constructor call cannot be translated into SQL.

+3
source

The Sql group and linq group return results of various forms.

Sql groupby returns keys and aggregates (without group members)

Linq groupby returns keys and group members.

If you use these group members, they must be (re) taken by the grouping key. This can lead to rounding +1 of the database for each group .

+3
source

Well, I had the same question a while ago. basically: your linq statement is converted to sql-statement. however: some groups will be translated, while others will depend on how you write your expression.
so yes - both are possible

Example:

 var a = (from entity in myTable where entity.Property == 1 select entity).ToList(); 

vs

 var a = (from entity in myTable.ToList() where entity.Property == 1 select entity).ToList(); 
+2
source

All Articles