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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:31:24+00:00 2026-06-14T06:31:24+00:00

I’m trying to build a single Or predicate from a list of predicates in

  • 0

I’m trying to build a single “Or” predicate from a list of predicates in the form List<Expression<Func<T, bool>>>

 public static IQueryable<T> Search<T>(this IQueryable<T> source, List<Expression<Func<T, bool>>> predicates = null)
            where T : EntityObject
        {
            if (predicates == null || predicates.Count == 0)
                return source;
            else if (predicates.Count == 1)
                return source.Where(predicates[0]);
            else
            {
                var row = Expression.Parameter(typeof(T), "row");
                var compoundExpression = predicates[0];

                for (int i = 1; i < predicates.Count; i++)
                {
                    compoundExpression = compoundExpression.Or(predicates[i]);
                }
                return source.Where(compoundExpression);
            }
        }

        static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> lhs, Expression<Func<T, bool>> rhs)
        {
            var row = Expression.Parameter(typeof(T), "row");
            var body = Expression.Or(
                Expression.Invoke(lhs, row),
                Expression.Invoke(rhs, row));
            return Expression.Lambda<Func<T, bool>>(body, row);
        }

But this is returning every row in my source?

For testing I am looking for c=>c.FullName.Contains("Smith") or c=>c.FullName.Contains("Jones")

I have tried amending to use PredicateBuilder but again it still returns every row in the source.

public static IQueryable<T> Search<T>(this IQueryable<T> source, List<Expression<Func<T, bool>>> predicates = null)
            where T : EntityObject
        {
            if (predicates == null || predicates.Count == 0)
                return source;
            else if (predicates.Count == 1)
                return source.Where(predicates[0]);
            else
            {
                var pb = PredicateBuilder.False<T>();
                for (int i = 0; i < predicates.Count; i++)
                {
                    pb = pb.Or(predicates[i]);
                }
                return source.AsExpandable().Where(pb);
            }
        }

Any assistance very gratefully received!

The end result would be to allow AND’s as well as OR’s
e.g. c=>c.FullName.Contains(“Dav”) AND c=>c.CustomerType == ‘Staff’

  • 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-14T06:31:25+00:00Added an answer on June 14, 2026 at 6:31 am

    Sometimes the weekend does strange things to code!

    Not sure what I’ve done differently, but this is working:

    public static IQueryable<T> Search<T>(this IQueryable<T> source, List<Expression<Func<T, bool>>> predicates = null)
                where T : EntityObject
            {
                if (predicates == null || predicates.Count == 0)
                    return source;
                else if (predicates.Count == 1)
                    return source.Where(predicates[0]);
                else
                {
                    var query = PredicateBuilder.False<T>();
                    foreach (var predicate in predicates)
                    {
                        query = query.Or(predicate);
                    }
    
                    return source.AsExpandable().Where(query);
                }
            }
    

    PredicateBuilder appears to be a great little bit of code.

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

Sidebar

Related Questions

public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text

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.