Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9117499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:54:34+00:00 2026-06-17T04:54:34+00:00

var costCenters = from c in dbContext.CostCenters //no sql call here orderby c.DisplayRank descending

  • 0
            var costCenters = from c in dbContext.CostCenters //no sql call here
                          orderby c.DisplayRank descending
                          select c;


            List<CostCenter> lstCostCenter = costCenters.ToList();//Immediate execution to sql the first time

            lstCostCenter = costCenters.ToList();//no Sql call ??

            int test = costCenters.Count();//Call Sql everytime
            test = costCenters.Count();//Call Sql again???

I am using Entity Framework 5

I’m starting to learn Linq. I am really confused as to which immediate execution functions will call to SQL everytime. As you can see in the above sample, both ToList() and Count() are immediate execution function, but only Count() will connect to sql on subsequence calls. ToList() connects to sql 1 time, but Count() will connect to Sql everytime.
How do I identify which linq functions will not get call to sql multiple times?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:54:35+00:00Added an answer on June 17, 2026 at 4:54 am

    The difference is the first time you call ToList() it converts the IQueryable(which is just a definition of a query) to a list of IEnumerable(querying the database). In other words you get back a list of objects that are now in memory, so any further LINQ calls on the resulting list use the IEnumerable version that works with in-memory objects. Additionally EF has a feature which caches results, so even if you call ToList on the original IQueryable reference, it is likely to use the in-memory objects instead of fetching them from the database. I am guessing that Count hits the database again instead of counting the cached results, because the query for count is not the same query for the ToList(it is a type of aggregate/grouping), and also perhaps it is designed that way because the DB engine is more efficient at providing a count.

    In your example, costCenters is an IQueryable, because all you do is define a query, but not call ToList yet. lstCostCenter is the IEnumerable representing the in-memory results from the query after being executed with ToList. Usually immediate calls that produce results, .Count, .ToList, etc. when performed on IQueryable will result in a DB call(the exception being when it finds cached results that it can reuse), and calls on the IEnumerable object(in your case lstCostCenter) will operate in-memory.

    To get more predictable results, then call ToList on the IQueryable first, and make all further calls on the IEnumerable. In other words, anything you call on lstCostCenter is guaranteed not to hit the database. This is usually the best way to handle it, except if you expect the resulting lists to be large. For example, if lstCostCenter ended up with a 10,000 objects, you probably wouldn’t then want to do lstCostCenter.Where(x=>x.Blah > 5) because that would loop over all 10,000 objects in memory to filter them. In such a case it would be better to modify the query first by appending the additional call to the IQueryable and then calling ToList so that we leverage the DB engine which is better at handling large sets: costCenters.Where(x=>x.Blah > 5).ToList().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

var q = (from Comments in db.tblBlogComments where Comments.blogID == this.ID orderby Comments.date descending
var q = from child in doc.Descendants(level) where (int)child.Attribute(id) == 55 select (string)child.Element(Points).**Value.ToString()**; I
var candidates = (from l in db.GetLeads(lat, lon, role, radius + 10) orderby l.Distance
var xEle = new XElement(ContentDetails, from emp in _lstContents select new XElement(Contents, new XAttribute(key,
var t = \some\route\here I need \some\route from it. Thank you.
var fruit = [apple,pear,pear,pear,banana]; How do I remove all pear fruit from this array?
var quantSubset = from userAns in userAnalysis.AllUserAnswers join ques in userAnalysis.AllSeenQuestions on userAns.QID equals
var seq = Enumerable.Range(1, 10).Reverse(); var sort1 = seq.OrderBy(i => i); var sort2 =
var query = from C in db.clients join O in db.orders on c.clientid equals
var query2 = (from p in db.posts where (p.date) == (from q in db.posts

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.