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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:28:39+00:00 2026-05-11T07:28:39+00:00

I have one queryable where I have used various Where and WhereBetween statements to

  • 0

I have one queryable where I have used various Where and WhereBetween statements to narrow the collection down to a certain set. Now I need to add kind of a Where || WhereBetween. In other words, I can’t just chain them together like I have up till now, cause that will work as an And. So, how can I do this?

I see two possibilities:

  1. Create two queryables from the one I have, one using the Where, and one using WhereBetween. And then concatenate them. Don’t know if this is even possible? Also, although not in my particular case, you would most likely end up with duplicates…
  2. Somehow merge the Where expression and the expression created in the WhereBetween with some sort of Or.

The first, as mentioned, I am not sure is even possible. And if it was, I’m not so sure it is a good way to do it.

The second, I can see as an option, but not totally sure about all the details. Below is the WhereBetween method from my other question, which I now use and it works great:

    public static IQueryable<TSource> WhereBetween<TSource, TValue>(         this IQueryable<TSource> source,         Expression<Func<TSource, TValue>> selector,         IEnumerable<Range<TValue>> ranges)     {         var param = Expression.Parameter(typeof(TSource), 'x');         var member = Expression.Invoke(selector, param);         Expression body = null;         foreach (var range in ranges)         {             var filter = Expression.AndAlso(                 Expression.GreaterThanOrEqual(member,                      Expression.Constant(range.A, typeof(TValue))),                 Expression.LessThanOrEqual(member,                      Expression.Constant(range.B, typeof(TValue))));             body = body == null ? filter : Expression.OrElse(body, filter);         }         return body == null ? source : source.Where(             Expression.Lambda<Func<TSource, bool>>(body, param));     } 

I’m thinking that I could maybe extract the expression building portion of it into a new method. Perhaps like this:

    public static IQueryable<TSource> WhereBetween<TSource, TValue>(         this IQueryable<TSource> source,         Expression<Func<TSource, TValue>> selector,         IEnumerable<Range<TValue>> ranges)     {         return source.Where(WhereBetween(selector, ranges));     }      public static Expression<Func<TSource, bool>> WhereBetween<TSource, TValue>(         Expression<Func<TSource, TValue>> selector,         IEnumerable<Range<TValue>> ranges)     {         var param = Expression.Parameter(typeof(TSource), 'x');         var member = Expression.Invoke(selector, param);         Expression body = null;         foreach (var range in ranges)         {             var filter = Expression.AndAlso(                 Expression.GreaterThanOrEqual(member,                      Expression.Constant(range.A, typeof(TValue))),                 Expression.LessThanOrEqual(member,                      Expression.Constant(range.B, typeof(TValue))));             body = body == null ? filter : Expression.OrElse(body, filter);         }         return body == null              ? ø => true             : Expression.Lambda<Func<TSource, bool>>(body, param);     } 

I could then use that new method to get the expression instead of the queryable. So, lets say I have the WhereBetween(ø => ø.Id, someRange) and for example ø => ø.SomeValue == null. How can I combine those two with Or? I’m looking at the Expression.OrElse used in the WhereBetween method, and I think that might be what I need, or maybe this the Expression.Or. But I’m very unstable on this expression stuff, so I am not sure what to choose here, or even if I am on the right track :p

Could someone give me some pointers here?

  • 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. 2026-05-11T07:28:39+00:00Added an answer on May 11, 2026 at 7:28 am

    You have two options here – Queryable.Union, or expression combination. I’d generally favor the latter, via OrElse – which (with LINQ-to-SQL at least) you can do with 2 expressions (see below) – but in either case it should get composed:

        using(var ctx = new DataClasses1DataContext())     {         ctx.Log = Console.Out;         Expression<Func<Customer, bool>> lhs =             x => x.Country == 'UK';         Expression<Func<Customer, bool>> rhs =             x => x.ContactName.StartsWith('A');          var arr1 = ctx.Customers.Where(             lhs.OrElse(rhs)).ToArray();          var arr2 = ctx.Customers.Where(lhs)             .Union(ctx.Customers.Where(rhs)).ToArray();     } 

    Both arr1 and arr2 each only perform 1 database hit (although the TSQL is different; the first has an OR in the WHERE clause; the second has two separate queries with UNION).

    Here’s the extension method I used:

    static Expression<Func<T, bool>> OrElse<T>(     this Expression<Func<T, bool>> lhs,     Expression<Func<T, bool>> rhs) {     var row = Expression.Parameter(typeof(T), 'row');     var body = Expression.OrElse(         Expression.Invoke(lhs, row),         Expression.Invoke(rhs, row));     return Expression.Lambda<Func<T, bool>>(body, row); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one master array and multi slave arrays and i need function that
I have a table that has multiple fields which I just need one field.
have one time consuming step that flattens a bunch of files. basically i'd like
i have one csv file which contains library group and it's data... group consider
I have one Sqlite Database file. I want to share my sqlite database between
I have one <asp:Image ID=imgBanner1 runat=server/> control in my aspx page, then in code
I have one method whose return type is void and it prints directly on
I have one issue with retrieval Parent/Child relationship of type Folder Hierarchy Ideally, in
I have one file example1.cpp with the main function. This file must have #include
I have one file one.php <?php //just a php function doen't have to do

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.