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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:18:59+00:00 2026-05-15T09:18:59+00:00

We have a project using LINQ to SQL, for which I need to rewrite

  • 0

We have a project using LINQ to SQL, for which I need to rewrite a couple of search pages to allow the client to select whether they wish to perform an and or an or search.

I though about redoing the LINQ queries using PredicateBuilder and have got this working pretty well I think. I effectively have a class containing my predicates, e.g.:

internal static Expression<Func<Job, bool>> Description(string term)
{
    return p => p.Description.Contains(term);
}

To perform the search i’m doing this (some code omitted for brevity):

public Expression<Func<Job, bool>> ToLinqExpression()
{
    var predicates = new List<Expression<Func<Job, bool>>>();
    // build up predicates here

    if (SearchType == SearchType.And)
    {
        query = PredicateBuilder.True<Job>();
    }
    else
    {
        query = PredicateBuilder.False<Job>();
    }

    foreach (var predicate in predicates)
    {
        if (SearchType == SearchType.And)
        {
            query = query.And(predicate);
        }
        else
        {
            query = query.Or(predicate);
        }
    }
    return query;
}

While i’m reasonably happy with this, I have two concerns:

  1. The if/else blocks that evaluate a SearchType property feel like they could be a potential code smell.
  2. The client is now insisting on being able to perform ‘and not’ / ‘or not’ searches.

To address point 2, I think I could do this by simply rewriting my expressions, e.g.:

internal static Expression<Func<Job, bool>> Description(string term, bool invert)
{
    if (invert)
    {
        return p => !p.Description.Contains(term);
    }
    else
    {
        return p => p.Description.Contains(term);
    }
}

However this feels like a bit of a kludge, which usually means there’s a better solution out there. Can anyone recommend how this could be improved? I’m aware of dynamic LINQ, but I don’t really want to lose LINQ’s strong typing.

  • 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-15T09:19:01+00:00Added an answer on May 15, 2026 at 9:19 am

    If you are looking for less lines you can replace the if/else with ternary operator:

    query = SearchType == SearchType.And ? PredicateBuilder.True<Job>() : PredicateBuilder.False<Job>();
    
       foreach (var predicate in predicates)
       {
            query = SearchType == SearchType.And ? query.And(predicate) : query.Or(predicate);
       }
    

    for the 'and not' / 'or not' part the ! operator should do the trick.

    PD: Did you test the foreach part is correctly setting the predicates?, as far as i remember you are building the expression that will be executed at later point in time, so you may have a literal reference just to the last set predicate in the final iteration, and that’s why you must use a temp variable to save the value of each iteration.

    EDIT:
    If you want to negate a expression programmatic, that’s a tricky one, you can try something like:

    internal static Expression<Func<Job, bool>> Description(string term, bool invert)
            {
               return NegateExp<Func<Job, bool>>(p => p.Description.Contains(term), invert);
            }
    

    And the NegateExp method will be something like:

    public static Expression<TDelegate> NegateExp<TDelegate>(Expression<TDelegate> expression, bool inverse)
            {
                if (inverse)
                {
                    return Expression.Lambda<TDelegate>(Expression.Not(expression.Body), expression.Parameters);
                }
                return expression;
            }
    

    You can take a look at this question for more examples Is there any way to negate a Predicate?

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

Sidebar

Ask A Question

Stats

  • Questions 514k
  • Answers 514k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You don't need to do any of that. You can… May 16, 2026 at 6:11 pm
  • Editorial Team
    Editorial Team added an answer The following works in Chrome 6.0.472.55/Ubuntu 10.04, and Firefox 3.6.9… May 16, 2026 at 6:11 pm
  • Editorial Team
    Editorial Team added an answer I've never heard of API hooking used to hook COM… May 16, 2026 at 6:11 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm using NerdDinner as a guide for my first MVC/LINQ to SQL project. It
I am currently using Dynamic Data Linq to SQL for a project. I was
I have a user defined function in a SQL Server 2005 database which returns
When manipulating data using Linq, how often does the SubmitChanges() method have to be
In my new project, I`m writing a lot of linq. And I have a
I have started using Ninject 2 (downloaded from Github yesterday including the MVC extension
I'll just jump right into it. I have a model called Request which I
I have a solution where i have 3 WPF projects under it (Project UI-A,
I have just updated to the latest structuremap dll and now my site no
I am about to start a new project and am deciding what data access

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.