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

  • Home
  • SEARCH
  • 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 1088863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:08:03+00:00 2026-05-16T23:08:03+00:00

(An earlier question, Recursively (?) compose LINQ predicates into a single predicate , is

  • 0

(An earlier question, Recursively (?) compose LINQ predicates into a single predicate, is similar to this but I actually asked the wrong question… the solution there satisfied the question as posed, but isn’t actually what I need. They are different, though. Honest.)

Given the following search text:

"keyword1 keyword2 ... keywordN"

I want to end up with the following SQL:

SELECT [columns] FROM Customer 
  WHERE (
        Customer.Forenames LIKE '%keyword1%' 
        OR 
        Customer.Forenames LIKE '%keyword2%'
        OR
        ...
        OR
        Customer.Forenames LIKE '%keywordN%'
    ) AND (
        Customer.Surname LIKE '%keyword1%' 
        OR 
        Customer.Surname LIKE '%keyword2%'
        OR
        ....
        OR
        Customer.Surname LIKE '%keywordN%'
    )

Effectively, we’re splitting the search text on spaces, trimming each token, constructing a multi-part OR clause based on each , and then AND’ing the clauses together.

I’m doing this in Linq-to-SQL, and I have no idea how to dynamically compose a predicate based on an arbitrarily-long list of subpredicates. For a known number of clauses, it’s easy to compose the predicates manually:

dataContext.Customers.Where(
    ( 
      Customer.Forenames.Contains("keyword1") 
      ||
      Customer.Forenames.Contains("keyword2")
    ) && (
      Customer.Surname.Contains("keyword1") 
      ||
      Customer.Surname.Contains("keyword2")
    )
);

In short, I need a technique that, given two predicates, will return a single predicate composing the two source predicates with a supplied operator, but restricted to the operators explicitly supported by Linq-to-SQL. Any ideas?

  • 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-16T23:08:03+00:00Added an answer on May 16, 2026 at 11:08 pm

    You can use the PredicateBuilder class

    IQueryable<Customer> SearchCustomers (params string[] keywords)
    {
      var predicate = PredicateBuilder.False<Customer>();
    
      foreach (string keyword in keywords)
      {
        // Note that you *must* declare a variable inside the loop
        // otherwise all your lambdas end up referencing whatever
        // the value of "keyword" is when they're finally executed.
        string temp = keyword;
        predicate = predicate.Or (p => p.Forenames.Contains (temp));
      }
      return dataContext.Customers.Where (predicate);
    }
    

    (that’s actually the example from the PredicateBuilder page, I just adapted it to your case…)


    EDIT:

    Actually I misread your question, and my example above only covers a part of the solution… The following method should do what you want :

    IQueryable<Customer> SearchCustomers (string[] forenameKeyWords, string[] surnameKeywords)
    {
        var predicate = PredicateBuilder.True<Customer>();
    
        var forenamePredicate = PredicateBuilder.False<Customer>();
        foreach (string keyword in forenameKeyWords)
        {
          string temp = keyword;
          forenamePredicate = forenamePredicate.Or (p => p.Forenames.Contains (temp));
        }
        predicate = PredicateBuilder.And(forenamePredicate);
    
        var surnamePredicate = PredicateBuilder.False<Customer>();
        foreach (string keyword in surnameKeyWords)
        {
          string temp = keyword;
          surnamePredicate = surnamePredicate.Or (p => p.Surnames.Contains (temp));
        }
        predicate = PredicateBuilder.And(surnamePredicate);
    
        return dataContext.Customers.Where(predicate);
    }
    

    You can use it like that:

    var query = SearchCustomers(
        new[] { "keyword1", "keyword2" },
        new[] { "keyword3", "keyword4" });
    
    foreach (var Customer in query)
    {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is very similar to an earlier question I asked ( This Question
In this earlier question , the OP asked for a data structure similar to
Question is similar to this , earlier question Which Dimitre has answered already. There
In an earlier question, I asked about typecasting pointers, but was directed to the
This is related to an earlier question I asked, where the answer was: If
I asked an earlier question about absolute and relative div positioning , but I
This question is an extension of a question I asked earlier: Python Delegate Pattern
This question is related to my earlier question, asked here: How do I get
I posted a question related to this topic earlier, but I'm having trouble figuring
In this earlier question the OP asked the following problem: Given a rectangular grid

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.