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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:58:14+00:00 2026-05-31T06:58:14+00:00

Please consider this code: System.Linq.Expressions.Expression<Func<tbl, bool>> exp_details = r => r.ID_Master == Id &&

  • 0

Please consider this code:

System.Linq.Expressions.Expression<Func<tbl, bool>> exp_details = r => r.ID_Master == Id &&
r.Year == Year &&
r.Month == Month ;

I want to write a function that expect some argument, and then retrieve some data from my database. The problem is I want to create dynamic condition.dor example if I pass IsDeleted argument with true value I want to add r.IsDeleted == true; to exp_details. How can I do this?

  • 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-31T06:58:15+00:00Added an answer on May 31, 2026 at 6:58 am

    The key to doing this is to use an ExpressionVisitor which walks the given expression and (optionally, in a subclass) allows for replacing recognized elements with others you specify. In my case this was done for ORM (NHibernate). This is what I use: (I’ll add references to my answer later)

    public class ParameterAssignAndReplacer : ExpressionVisitor
    {
        private readonly ParameterExpression _source;
        private readonly ConstantExpression _target;
    
        internal ParameterAssignAndReplacer(ParameterExpression source, ConstantExpression target)
        {
            _source = source;
            _target = target;
        }
    
        protected override Expression VisitParameter(ParameterExpression node)
        {
            return node.Name == _source.Name ?
                base.VisitConstant(_target) :
                base.VisitParameter(node);
        }
    }
    

    And..

    public static class ExpressionExtensions
    {
        public static Expression<Func<TArg2, TResult>> AssignAndReduce<TArg1, TArg2, TResult>(
            this Expression<Func<TArg1, TArg2, TResult>> func,
            TArg1 parameter)
        {
            var exprBody = func.Body;
            var assignedParameter = Expression.Constant(parameter, typeof(TArg1));
            exprBody = new ParameterAssignAndReplacer(func.Parameters[0], assignedParameter).Visit(exprBody);
            return Expression.Lambda<Func<TArg2, TResult>>(exprBody, func.Parameters[1]);
        }
    }
    

    Both classes can be extended to your specific scenario. To make this relevant to the code you posted (I’m using only IsDeleted as the parameter for simplicity):

    public class SomeClass
    {
        Expression<Func<tbl, bool, bool>> _templateExpression =
           (tbl r, bool isDeleted) => r.ID_Master == 5 && r.Year == 2008 && r.Month == 12 && r.IsDeleted == isDeleted;
    
        public Expression<Func<tbl, bool>> Foo(bool IsDeleted)
        {
            return _templateExpression.AssignAndReduce(IsDeleted);
        }
    }
    

    Regarding references, most of what I learned on this topic is from Marc Gravell’s “Expressions” answers [ although many other users have helped me get a grip on this 🙂 ]

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

Sidebar

Related Questions

Please consider this code: template<typename T> char (&f(T[1]))[1]; template<typename T> char (&f(...))[2]; int main()
I am trying to use R2WinBUGS using this example: code (Please only consider the
Please consider this code: #include <iostream> template<typename T> void f(T x) { std::cout <<
Please consider the following piece of code, which throws three different exceptions (namely, System.Configuration.ConfigurationErrorsException
Please consider this code. Is it using Circular Reference? If not why am I
Please consider this code snippet: private static void doSomething(Double avg, Double min, Double sd)
Please consider this code snippet: var ul = $(.list_b).find(li).remove().end(); $.each(Sites, function (index, value) {
Please consider this code: NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:[ NSURL URLWithString:url ]]; //
Please consider this code: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup
Please, consider the following (I'm sorry for the amount of code; but this is

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.