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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:49:27+00:00 2026-05-11T18:49:27+00:00

I am using my own extension methods of IQueryable<> to create chainable queries such

  • 0

I am using my own extension methods of IQueryable<> to create chainable queries such as FindAll().FindInZip(12345).NameStartsWith(“XYZ”).OrderByHowIWantIt() etc. which then on deferred execution creates a single query based on my extension methods chain.

The problem with this though, is that all Where’s in the extension chain (FindXYZ, FindInZip etc.) will always combine as AND which means I can’t do something like this:

FindAll().FirstNameStartsWith(“X”).OrLastNameStartsWith(“Z”) because I don’t know how I can inject the OR in a separate Where method.

Any idea how I can solve this?


additional;
So far I understand how to chain expressions as Or if I wrap them (e.g. CompileAsOr(FirstNameStartsWith(“A”).LastNameStartsWith(“Z”).OrderBy(..))

What I’m trying to do though is slightly more complicated (and PredicateBuilder doesn’t help here..) in that I want a later IQueryable to basically access the Where conditions that were established prior without having to wrap them to create the Or between them.

As each extension method returns IQueryable<> I understand that it should have the knowledge about the current status of query conditions somewhere, which leads me to believe that there should be some automated way or creating an Or across all prior Where conditions without having to wrap what you want Or’d.

  • 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-11T18:49:27+00:00Added an answer on May 11, 2026 at 6:49 pm

    I’m assuming the different parts of the query are only known at runtime, i.e. you can’t just use || in a where…

    One lazy option is Concat – but this tends to lead to poor TSQL etc; however, I tend to be inclined to write custom Expressions instead. The approach to take depends on what the provider is, as LINQ-to-SQL supports different options to EF (for example) – which has a genuine impact here (since you can’t use sub-expressions with EF). Can you tell us which?


    Here’s some code that should work with LINQ-to-SQL; if you build an array (or list, and call .ToArray()) of expressions, it should work fine; example is LINQ-to-Objects, but should still work:

        static void Main()
        {
            var data = (new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).AsQueryable();
    
            var predicates = new List<Expression<Func<int, bool>>>();
            predicates.Add(i => i % 3 == 0);
            predicates.Add(i => i >= 8);           
    
            foreach (var item in data.WhereAny(predicates.ToArray()))
            {
                Console.WriteLine(item);
            }
        }
    
        public static IQueryable<T> WhereAny<T>(
            this IQueryable<T> source,
            params Expression<Func<T,bool>>[] predicates)
        {
            if (source == null) throw new ArgumentNullException("source");
            if (predicates == null) throw new ArgumentNullException("predicates");
            if (predicates.Length == 0) return source.Where(x => false); // no matches!
            if (predicates.Length == 1) return source.Where(predicates[0]); // simple
    
            var param = Expression.Parameter(typeof(T), "x");
            Expression body = Expression.Invoke(predicates[0], param);
            for (int i = 1; i < predicates.Length; i++)
            {
                body = Expression.OrElse(body, Expression.Invoke(predicates[i], param));
            }
            var lambda = Expression.Lambda<Func<T, bool>>(body, param);
            return source.Where(lambda);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using my own class for database queries, extending mysqli: class iDatabase extends mysqli
A contractor where I work is using extension methods to implement CRUD on well-known
I'm using a third party library which contains a bunch of extension methods on
What is the simplest way to create an extension function or method using Jquery.
I am supposed to create my own 'reliable transport protocol' using UDP and written
after using own custom cms for over 6 years, I decided to go for
I am using my own custom authentication with IIS, and I want the server
I am using my own implementation of the UserDetailsService interface to load a User
I am using my own custom navigationBar, but i need to access it in
I am using my own MVC implementation and I am not sure, whether the

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.