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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:59:55+00:00 2026-06-07T00:59:55+00:00

So I have a fairly standard LINQ-to-Object setup. var query = expensiveSrc.Where(x=> x.HasFoo) .OrderBy(y

  • 0

So I have a fairly standard LINQ-to-Object setup.

var query = expensiveSrc.Where(x=> x.HasFoo)
                        .OrderBy(y => y.Bar.Count())
                        .Select(z => z.FrobberName);    

// ...

if (!condition && !query.Any())
 return; // seems to enumerate and sort entire enumerable 

// ...

foreach (var item in query)
   // ...

This enumerates everything twice. Which is bad.

var queryFiltered = expensiveSrc.Where(x=> x.HasFoo);

var query = queryFiltered.OrderBy(y => y.Bar.Count())
                         .Select(z => z.FrobberName); 

if (!condition && !queryFiltered.Any())
   return;

// ...

foreach (var item in query)
   // ...

Works, but is there a better way?

Would there be any non-insane way to “enlighten” Any() to bypass the non-required operations? I think I remember this sort of optimisation going into EduLinq.

  • 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-07T00:59:57+00:00Added an answer on June 7, 2026 at 12:59 am

    There is not much information that can be extracted from an enumerable, so maybe it’s better to turn the query into an IQueryable? This Any extension method walks down its expression tree skipping all irrelevant operations, then it turns the important branch into a delegate that can be called to obtain an optimized IQueryable. Standard Any method applied to it explicitly to avoid recursion. Not sure about corner cases, and maybe it makes sense to cache compiled queries, but with simple queries like yours it seems to work.

    static class QueryableHelper {
        public static bool Any<T>(this IQueryable<T> source) {
            var e = source.Expression;
            while (e is MethodCallExpression) {
                var mce = e as MethodCallExpression;
                switch (mce.Method.Name) {
                    case "Select":
                    case "OrderBy":
                    case "ThenBy": break;
                    default: goto dun;
                }
                e = mce.Arguments.First();
            }
            dun:
            var d = Expression.Lambda<Func<IQueryable<T>>>(e).Compile();
            return Queryable.Any(d());
        }
    }
    

    Queries themselves must be modified like this:

    var query = expensiveSrc.AsQueryable()
                            .Where(x=> x.HasFoo)
                            .OrderBy(y => y.Bar.Count())
                            .Select(z => z.FrobberName); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have what I assume is a fairly standard setup, with one scratchpad MOC
I have a fairly standard layout for the first screen of the setup wizard
I have a fairly standard setup where a front-end Apache server forwards requests to
I have a fairly standard OTP setup with rebar and reltool. I've setup reltool
I have a fairly standard search query to find related content. Among other things,
I have what I think is a fairly standard setup, a ListBox backed by
I have a fairly standard inheritance situation in my current LINQ-to-SQL project. I have
I have a fairly standard OLTP normalised database and I have realised that I
I have a fairly standard .Net MVC Controller method: public ActionResult Add(Customer cust) {
We have a fairly standard release procedure using Visual Source Safe for labeling 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.