.NET – How to construct the performance of the LINQ expression?

Is there a difference in performance based on the position of the where clause in the linq expression?

Look at the following code:

dbContext.AnEntity.Include("AnotherEntity").Where(e => e.ID = = id).ToList();

dbContext.AnEntity.Where(e => e.ID == id).Include("AnotherEntity").ToList();

Are the execution plans of these two expressions the same or different?

In theory, no. In fact, you need to provide Test. For related (but slightly different) examples, which have long existed in L2S, .Where (predicate).FirstOrDefault() and .FirstOrDefault (predicate) (with the same semantics) do not have the same behavior (especially related )Identity mapping shortcut).

I hope it will be the same, especially because .Include (extended result set) is a separate category than .Where (a predicate) but: verification The only way is to test it and compare the generated SQL and performance.

According to the position of the where clause in the linq expression, is there a difference in performance?

Look at the following code:

dbContext.AnEntity.Include("AnotherEntity").Where(e => e.ID = = id).ToList();

dbContext.AnEntity.Where(e => e.ID == id).Include("AnotherEntity").ToList();

Are the execution plans of these two expressions the same or different?

In theory, no. In fact, you need to test according to the specific situation and the provider. For related (but slightly different) For example, in L2S for a long time, .Where (predicate).FirstOrDefault() and .FirstOrDefault (predicate) (have the same semantics) do not have the same behavior (especially related identity mapping shortcuts).

I hope it will be the same, especially because .Include (extended result set) is a separate category than .Where (a predicate) But: the only way to verify is to test it and compare the generated SQL and Performance.

Leave a Comment

Your email address will not be published.