Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AsNoTracking using LINQ Query syntax instead of Method syntax

I'm interested in using AsNoTracking with my LINQ select queries to improve performance. I'm using Entity Framework 5 with Code First.

However, all of my queries are written using LINQ Query syntax and all of the AsNoTracking examples are shown using the Method syntax. I'm aware that AsNoTracking was created for the Method syntax but how do I achieve the same thing with the Query syntax?

like image 209
Mitch Avatar asked Aug 08 '13 09:08

Mitch


People also ask

Does Linq support method syntax?

Linq namespace generally uses method syntax.

What is the use of AsNoTracking in Linq?

The AsNoTracking() extension method returns a new query and the returned entities will not be cached by the context (DbContext or Object Context). This means that the Entity Framework does not perform any additional processing or storage of the entities that are returned by the query.

What is method syntax in Linq C#?

In LINQ, Method Syntax is used to call the extension methods of the Enumerable or Queryable static classes. It is also known as Method Extension Syntax or Fluent. However, the compiler always converts the query syntax in method syntax at compile time.


2 Answers

You apply AsNoTracking() to the DbSet:

var result = (
    from person in ctx.People.AsNoTracking()
    select person)
    .ToList();
like image 163
qujck Avatar answered Oct 02 '22 23:10

qujck


Query syntax is replaced with method syntax by compiler, so there is no difference at all at the end.

like image 45
MarcinJuraszek Avatar answered Oct 02 '22 23:10

MarcinJuraszek