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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:44:33+00:00 2026-06-15T18:44:33+00:00

If I chain clauses such as var results = elements .Where(n => n >

  • 0

If I chain clauses such as

var results = elements
    .Where(n => n > 3)
    .Where(n => n % 2 == 0);

is this slower than just

var results = elements.Where(n => n > 3 && n % 2 == 0);

Explain why or why not?

EDIT: It seems that the consensus is that even POCO objects iterate twice. If this is the case can someone explain why Microsoft wouldn’t combine these predicates. I stumbled across Enumerable.CombinePredicates that I thought did this. Can someone please explain what this does then.

  • 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-15T18:44:34+00:00Added an answer on June 15, 2026 at 6:44 pm

    Edit:
    I looked a little closer. The WhereEnumerableIterator returned by the Where extension method actually overrides the Where method and combines the predicates into a single callback.

    public override IEnumerable<TSource> Where(Func<TSource, bool> predicate) {
        return new Enumerable.WhereEnumerableIterator<TSource>(
            this.source, 
            Enumerable.CombinePredicates<TSource>(this.predicate, predicate));
    }
    
    private static Func<TSource, bool> CombinePredicates<TSource>(
        Func<TSource, bool> predicate1, Func<TSource, bool> predicate2
        ) {
        return (TSource x) => predicate1(x) && predicate2(x);
    }
    

    So, the speed difference I saw on my machine should probably be attributed to something else.


    The first example will loop over the elements collection once to find items that satisfy the condition item > 3, and again to find items that satisfy the condition item % 2 == 0.

    The second example will loop over the elements collection once to find items that satisfy the condition item > 3 && item % 2 == 0.

    In the examples provided, the second will most likely always be faster than the first, because it only loops over elements once.

    Here is an example of some pretty consistent results I get on my machine (.NET 3.5):

        var stopwatch = new System.Diagnostics.Stopwatch();
        var elements = Enumerable.Range(1, 100000000);
        var results = default(List<int>);
        stopwatch.Start();
        results = elements.Where(n => n > 3).Where(n => n % 2 == 0).ToList();
        stopwatch.Stop();
        Console.WriteLine(stopwatch.Elapsed);
        stopwatch.Reset();
        stopwatch.Start();
        results = elements.Where(n => n > 3 && n % 2 == 0).ToList();
        stopwatch.Stop();
        Console.WriteLine(stopwatch.Elapsed);
        Console.WriteLine("Done");
        Console.ReadLine();
    

    Results:

    00:00:03.0932811
    00:00:02.3854886
    Done
    

    EDIT:
    @Rawling is right in that my explanation only applies to LINQ as used on collections of POCO objects. When used as an interface to LINQ-to-SQL, NHibernate, EF, etc., your results will be more implementation-dependent.

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

Sidebar

Related Questions

My Situation: The data request chain of my application looks like this: (Client) ->
I just began reading more about Markov Chain Generators today, and am really intrigued
Is it possible in jQuery to daisy-chain selectors like so? var sData = $('#myTableRow
I am trying to implement the chain of responsibility pattern, but it seems i
I am trying to write a dynamic search I and chain where clauses I
Media Browser has a provider model, this is basically a chain of classes that
The main documentation describe it at debugging callbacks but it seems to not exists:
Example: print max(chain_length(i) for i in xrange(1,10001)) This returns the maximum/biggest chain_length (an arbitrary
I search a tool(chain) (sed/bash/...) in Linux, to achieve the following: Go through a
So I have a really long chain of methods, something similar to: return self.append_command(fbghasjfa).append_command(input_file_part).append_command(output_video_codec_part).append_command(output_resolution_part).append_command(output_video_bitrate_part).append_command(strict_part).append_command(output_audio_codec_part).append_command(output_number_of_audio_channels_part).append_command(output_audio_bitrate_part).append_command(output_file_part).__finalized

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.