Blog

Filter posts by the Tags of Blog section!

Note about the Deferred Execution in LINQ component Thursday, 12 May 2016

Maybe you know about the deferred execution unconsciously. This sexy phrase refers to the point of LINQ queries when it executed. Although executing the LINQ query should be considered and after doing join, group, let and every LINQ instructions, finally, it should be executed; because it will be executed in every call if you don't execute it! Take a look at the following example:

  public IEnumerable<Blog> GetAllPosts()

        {

            var executedQuery =_siteContext.Blogs.

                Where(p => p.IsDeleted == false && p.IsPublished)

                .OrderByDescending(c => c.CreationDate)

                .Include(c => c.Tags)

                .ToList();

           var count = executedQuery .Count();

           var average= executedQuery.Contains("d");

           return executedQuery ;

        }

As you can see the executed Query has been executed by .ToList() and the Count() and Contains("d") will be queried in memory and If you don't call .ToList() it will be executed in each of the mentioned LINQ methods.

Tags: Linq C#

comments powered by Disqus