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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:58:48+00:00 2026-06-18T18:58:48+00:00

Not sure if this is entirely possible; I’ve found some stuff about expression and

  • 0

Not sure if this is entirely possible; I’ve found some stuff about expression and predicate builders, but so far nothing that lets you run arbitrary queries without knowing them in advance.

Basically, I have a collection of objects from a large SQL database, and I’m building a webpage (ASP.NET MVC 4) to allow a user to display and filter these objects. The queries the users will be entering will vary in complexity. The simplest and neatest way to let them enter these queries is something like the way the Visual Studio TFS plugin lets you search work items: a table of conditions, where you can keep adding rows. You select “and” or “or” for the join condition, then select a field, enter a value, and select whether you want things that do or do not match it:

1. show items where [Field] [is|is not] [value]
2.         [and|or] [Field] [is|is not] [value]
3.         [and|or] [Field] [is|is not] [value]
etc...

What’s the simplest way to turn that into something LINQ-ish that I can stick a .ToList() on the end of? The only solution I’ve come up with so far has involved a rather large and ugly switch block with cases to match the various fields and tack on a .Where(), but to allow the user to select “or” for a condition then I end up doing something like this:

  • While condition is AND:
    • use big switch to match the field
    • query = query.Where(ThisField == value);
  • When you hit a condition that is OR:
    • append current results to temporary list
    • new query from full unfiltered list
    • use big switch to match the field
    • query = fullList.Where(ThisField == value);
    • continue as before
  • When you run out of conditions, append your current result set to the temporary list you’ve been using all along, and return that list.

This seems less elegant than I’d like.

  • 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-18T18:58:50+00:00Added an answer on June 18, 2026 at 6:58 pm

    You can do it like this:

    class Program
    {
        public enum Operator
        {
            And,
            Or
        }
    
        public class Condition
        {
            public Operator Operator { get; set; }
            public string FieldName { get; set; }
            public object Value { get; set; }
        }
    
        public class DatabaseRow
        {
            public int A { get; set; }
            public string B { get; set; }
        }
    
        static void Main(string[] args)
        {
            var conditions = new List<Condition>
            {
                new Condition { Operator = Operator.And, FieldName = "A", Value = 1 },
                new Condition { Operator = Operator.And, FieldName = "B", Value = "Asger" },
                new Condition { Operator = Operator.Or, FieldName = "A", Value = 2 },
            };
    
            var parameter = Expression.Parameter(typeof (DatabaseRow), "x");
            var currentExpr = MakeExpression(conditions.First(), parameter);
            foreach (var condition in conditions.Skip(1))
            {
                var nextExpr = MakeExpression(condition, parameter);
                switch (condition.Operator)
                {
                    case Operator.And:
                        currentExpr = Expression.And(currentExpr, nextExpr);
                        break;
                    case Operator.Or:
                        currentExpr = Expression.Or(currentExpr, nextExpr);
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
            }
    
            var predicate = Expression.Lambda<Func<DatabaseRow, bool>>(currentExpr, parameter).Compile();
    
            var input = new[]
            {
                new DatabaseRow {A = 1, B = "Asger"},
                new DatabaseRow {A = 2, B = "Hans"},
                new DatabaseRow {A = 3, B = "Grethe"}
            };
    
            var results = input.Where(predicate).ToList();
        }
    
        static BinaryExpression MakeExpression(Condition condition, ParameterExpression parameter)
        {
            return Expression.Equal(
                Expression.MakeMemberAccess(parameter, typeof (DatabaseRow).GetMember(condition.FieldName)[0]),
                Expression.Constant(condition.Value));
        }
    }
    

    This assumes you have a class as a model of the database row with the correct types. You can then parse your conditions into a list of the typed condition shown above via regex and the provided code can convert that to an expression tree. The resulting expression can either be compiled and run (as shown) or converted to SQL (just stuffing the predicate into the IQueryable.Where instead).

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

Sidebar

Related Questions

I am not entirely sure if this is possible or not but so far
I'm not entirely sure if this is possible and tried searching but couldn't find
I'm not entirely sure this is possible, but I would like to simply shorten
I'm not entirely sure if this is possible, but I'm trying to create a
I'm not entirely sure that this is possible, but what I'm hoping to accomplish
I'm not entirely sure if this is even possible, but I'll try my best
so I'm not entirely sure this is possible, but I am curious if it
I'm not entirely sure if this is possible, but does anyone know how, or
Hi am not entirely sure if this is possible but appreciate any information given,
Not entirely sure this is possible, but hoping to be pleasantly surprised. I have

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.