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 7016619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:48:33+00:00 2026-05-27T22:48:33+00:00

I query a database and push the result out to the console and a

  • 0

I query a database and push the result out to the console and a file using two methods like this:

var result = from p in _db.Pages
             join r in rank on p.PkId equals r.Key
             orderby r.NumPages descending
             select new KeyNameCount
                                    {
                                        PageID = p.PkId,
                                        PageName = p.Name,
                                        Count = r.NumPages
                                    };

WriteFindingsToFile(result, parentId);
WriteFindingsToConsole(result, parentId);

IEnumerable<T>, not IQuerable<T> is used as parametertype for result when used as a parameter in the two method calls.

In both calls the result is iterated in a foreach. This leads to two identical calls against the database, one for each method.

I could refactor the two methods into one and only use one foreach, but that would fast become very hard to maintain (adding write to html, write to xml, etc.)

I am pretty sure this is a farly common question, but using google has not made me any wiser, so I turn to you guys 🙂

  • 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-05-27T22:48:34+00:00Added an answer on May 27, 2026 at 10:48 pm

    Every time you access a LINQ query it will requery the database to refresh the data. To stop this happening use .ToArray() or .ToList().

    e.g.

    var result = (from p in _db.Pages
                 select p).ToList(); //will query now
    
    Write(result);  //will not requery
    Write(result2); //will not requery
    

    It’s important to understand that a raw LINQ query is run when it is used, not when it is written in the code (e.g. don’t dispose of your _db before then).

    It can be surprising when you realise how it really works. This allows method chaining and later modification of the query to be reflected in the final query run on the DB. It is important to always keep in mind as it can cause run-time bugs that will not be caught at compile time, usually because the DB connection is closed before the list is used, as you are passing around what appear to be a simple IEnumerable.

    EDIT: Changed to remove my opinion and reflect the discussion in the comments. Personally I think the compiler should assume that the end result of chained queries is immediately run unless you explicitly say that it’ll be further modified later. Just to avoid the run-time bugs it inevitably causes.

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

Sidebar

Related Questions

I do this to query all data from the database $query=$this->db->get('tablename', $num, $offset); I
I'm running a database query to load a dropdownbox using jquery. Is there a
I'm trying to figure out two things: Can Xpath be used to query a
I am using this category script: <?php include(connect.php); $nav_query = mysql_query(SELECT * FROM `categories`
What is the best way to push database changes to silverlight ui using net.tcp
Is there a quick way to query a database table in c# and push
what i am attempting to build is an array from a database query in
I have to create JSON string from database values and push it back to
I need to query a database which is designed so what a NVARCHAR column
I need to query the database to get the column names , not to

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.