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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:24:00+00:00 2026-06-17T01:24:00+00:00

I’ve been reading that Ienumerables don’t run straight away. So I’m trying to find

  • 0

I’ve been reading that Ienumerables don’t run straight away. So I’m trying to find the best way to query a list.

Below is my getAll method. Followed by my filter method. Followed by a preferred filter method (Readability).

My question is, will the 3rd method be the same as querying directly? In other words, will it, or won’t it Load ALL myObjects from the DB and then filter? Or filter when getting from the DB.

   public static IEnumerable<myObject> getAll()
    {
        using (var db = Database.OpenConnectionString(blahblah))
        {
            var queryResults = db.Query("SELECT * FROM vu_myObjects");

            return queryResults .Select(x => new myObject(x.myObjectName, x.myObjectF));
        }
    }

    public static IEnumerable<myObject> getAllForFilter(String filter)
    {
        using (var db = Database.OpenConnectionString(blahblah))
        {
            var queryResults  = db.Query("SELECT * FROM vu_myObjects WHERE ObjectF = @0", filter);

            return queryResults.Select(x => new myObject(x.myObjectName, x.ObjectF));
        }
    }

   public static IEnumerable<myObject> getAllForFilter(String filter)
    { // Not checked syntax.
        return getAll().Where(x => x.ObjectF = filter);
    }

Alternatively, if you have a better suggestion, I’m all ears.

  • 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-17T01:24:02+00:00Added an answer on June 17, 2026 at 1:24 am

    will it, or wont it Load ALL myObjects from the DB and then filter?

    It won’t as you haven’t actually queried the database yet. IQueryable<T> has deferred exection therefore in order to actually return a result set you need to execute the query. This can be done in a couple of ways e.g. calling ToList or iterating the list.

    Any filtering i.e. your Where clause, would be done DB side.

    Actually, the code you have at the moment won’t even work – it would throw an ObjectDisposedException because you are trying to apply a filter on a delayed query after disposing the data context. For your code to work you would need to either pass in the where clause as a parameter to your getAll method e.g.

    public static IEnumerable<myObject> getAll(Expression<Func<myObject,bool>> where)
    {
        using (var db = Database.OpenConnectionString(blahblah))
        {
            var queryResults = db.Query("SELECT * FROM vu_myObjects");
            if (where != null)
            {
                queryResults.Where(where);
            }
            return queryResults .Select(x => new myObject(x.myObjectName, x.myObjectF));
        }
    }
    
    public static IEnumerable<myObject> getAllForFilter(String filter)
    { // Not checked syntax.
        return getAll(x => x.ObjectF = filter);
    }
    

    Update

    As you have just pointed out that you are using WebMatrix.Database none of the above applies as there is no deferred execution happening here. The Query method executes immediately and returns a result set of IEnumerable<T> so any filtering applied afterwords would be done in memory. So in terms of efficiency, the first filter method would be better as it does everything at the DB side.

    You may be able to get both readability & efficiency by converting the Where clause to the actual string and appending it to your query.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I am doing a simple coin flipping experiment for class that involves flipping a

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.