Shahryar Saljoughi
Shahryar Saljoughi

Reputation: 3059

When is data really retrieved from database using EF Core?

To retrieve data, first I wrote LINQ query, that I expect not to executed on database until I call the FirstAsync() method.

var query =
            from tkn in Db.Set<TableA>()
            where tkn.IsActive == true
            where tkn.Token == token
            select tkn.RelatedObjectMappedToTableB;

var retrievedObject = await clientQuery.FirstAsync();

The problem is while debugging I can see the values of the related object in the Watch of visual studio, before reaching the call to FirstAsync()
This means to me that database is already queried and EF has not waited until I ask it to do so.

Why is it doing so? Is my opinion about when a linq query is executed wrong?

Upvotes: 1

Views: 282

Answers (2)

Camilo Terevinto
Camilo Terevinto

Reputation: 32068

This means to me that database is already queried and ef has not waited until I ask it to do so.

No, the database is being queried exactly because you asked it to do so.

Is my opinion about when a linq query is executed wrong?

No, it is not, however,

Why is it doing so?

LINQ retrieves the values in a lazy way, which means that it waits until you perform some enumeration on it. Using Visual Studio's Watch Window is asking the query to be evaluated so you can see the actual details.

Upvotes: 5

habib
habib

Reputation: 2446

LINQ support two execution behaviors deferred execution and immediate execution. Deferred execution means that the evaluation of an expression is delayed until its realized value is actually required. It greatly improves performance by avoiding unnecessary execution.

Deferred execution is applicable to any in-memory collection as well as remote LINQ providers like LINQ-to-SQL, LINQ-to-Entities, LINQ-to-XML, etc.

In the provided example in question, it seems that query executed at where but that's not happening actually. The query executed when you call .FirstAsync(), It means you really need data and C# execute the query, extract data and store data into memory.

If you want immediate execution behavior you can use .ToList() it will extract the complete projected data and save into memory.

var result =(from tkn in Db.Set<TableA>()
            where tkn.IsActive == true
            & tkn.Token == token
            select tkn.RelatedObjectMappedToTableB).ToList();

For more details see article

Upvotes: 0

Related Questions