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?
Linq namespace generally uses method syntax.
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.
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.
You apply AsNoTracking()
to the DbSet
:
var result = (
from person in ctx.People.AsNoTracking()
select person)
.ToList();
Query syntax is replaced with method syntax by compiler, so there is no difference at all at the end.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With