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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:09:01+00:00 2026-06-06T02:09:01+00:00

I am busy writing an dynamic AND filter on a IQueryable Linq object, so

  • 0

I am busy writing an dynamic AND filter on a IQueryable Linq object, so far this is my code and it works:

public static IQueryable<T> FilterHeaders<T>(this IQueryable<T> records, IEnumerable<ListHeader> headers)
{
    // Build linq expression to filter queryable
    if (headers != null)
    {
        var param = Expression.Parameter(typeof(T), "x");
        var body = Expression.And(Expression.Constant(true), Expression.Constant(true));

        foreach (var header in headers)
        {
            if (header.Filter != null && !String.IsNullOrWhiteSpace(header.Filter.Value))
            {
                var property = Expression.PropertyOrField(param, header.HeaderType.ToString());
                var value = Expression.Constant(header.Filter.Value.Trim(), typeof(string));
                body = Expression.AndAlso(body, Expression.Call(property, "Contains", null, value));
            }
        }
        var lambda = Expression.Lambda<Func<T, bool>>(body, param);
        return records.Where(lambda);
    }
    return records;
}

I have initialized my expression body with Expression.And(Expression.Constant(true), Expression.Constant(true)). It seems to me that there should be a better way…

How?

  • 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-06T02:09:05+00:00Added an answer on June 6, 2026 at 2:09 am

    Can’t you just do:

    var body = Expression.Constant(true);
    

    Note that you don’t have to use AndAlso only after And, despite the confusing names.
    The difference between them is much like the difference between & and && – AndAlso is a logical And operator, and And is a bitwise operator.

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

Sidebar

Related Questions

I'm writing my code in Haxe. This is quite irrelevant to the question though,
I'm busy writing a BHO(Browser Helper Object) in C# and I need to attach
I am busy writing code to use a third party service to process payments.
I'm busy writing a servlet that generates HTML code. The goal is to create
I am busy writing my thesis (so, I guess this could count as a
I'm busy writing a application that refreshes a IFrame completely using javascript, now my
I've been using Bamboo for 3 weeks and I'm busy writing a plugin to
I am busy writing a login page in Silverlight. I am using an Authentication
I am busy writing a CodeSmith template that has one of its properties as
I'm busy writing an application to run on an android device. How can I

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.