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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:53:42+00:00 2026-06-12T23:53:42+00:00

I have built a repository using Lambda expressions to filter my entity collections. As

  • 0

I have built a repository using Lambda expressions to filter my entity collections. As a parameter to the method I’m sending Expression<Func<Case, bool>> exp. But inside the method I would like to update that same expression with some global filters. I can see that the expression object itself got an Update method, but I can’t figure out how it is implemented (and cannot find anything while searching the net).

exp.Update(exp.Body, ???);

Could anyone give an example??

EDIT: Definition of the method: http://msdn.microsoft.com/en-us/library/ee378255.aspx

EDIT2: This is my code (where I try to use .And):

Expression<Func<Case, bool>> newExp = c => c.CaseStatusId != (int)CaseStatus.Finished
var binExp = Expression.And(exp.Body, newExp.Body);
ParameterExpression paramExp = Expression.Parameter(typeof(Expression<Func<Case, bool>>), "c");
return repository.Where(Expression.Lambda<Expression<Func<Case, bool>>>(binExp, 
    new[] { paramExp }).Compile()).ToArray();

It fails with the following ArgumentException: Lambda type parameter must be derived from System.Delegate

  • 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-12T23:53:43+00:00Added an answer on June 12, 2026 at 11:53 pm

    I don’t think the Update method can help you here. It only creates a new lambda, but does not update the original parameters with the new one, you have to do it manually.
    I’d recommend having a visitor that replaces the parameter, then you can And the expressions together.

    In total you would get something like:

        private Case[] getItems(Expression<Func<Case, bool>> exp)
        {
            return repository.Where(AddGlobalFilters(exp).Compile()).ToArray();
        }
    
        private Expression<Func<Case, bool>> AddGlobalFilters(Expression<Func<Case, bool>> exp)
        {
            // get the global filter
            Expression<Func<Case, bool>> newExp = c => c.CaseStatusId != (int)CaseStatus.Finished;
    
            // get the visitor
            var visitor = new ParameterUpdateVisitor(newExp.Parameters.First(), exp.Parameters.First());
            // replace the parameter in the expression just created
            newExp = visitor.Visit(newExp) as Expression<Func<Case, bool>>;
    
            // now you can and together the two expressions
            var binExp = Expression.And(exp.Body, newExp.Body);
            // and return a new lambda, that will do what you want. NOTE that the binExp has reference only to te newExp.Parameters[0] (there is only 1) parameter, and no other
            return Expression.Lambda<Func<Case, bool>>(binExp, newExp.Parameters);
        }
    
    
        /// <summary>
        /// updates the parameter in the expression
        /// </summary>
        class ParameterUpdateVisitor : ExpressionVisitor
        {
            private ParameterExpression _oldParameter;
            private ParameterExpression _newParameter;
    
            public ParameterUpdateVisitor(ParameterExpression oldParameter, ParameterExpression newParameter)
            {
                _oldParameter = oldParameter;
                _newParameter = newParameter;
            }
    
            protected override Expression VisitParameter(ParameterExpression node)
            {
                if (object.ReferenceEquals(node, _oldParameter))
                    return _newParameter;
    
                return base.VisitParameter(node);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have built a local DVD Database using Codeigniter, With film names etc in.
I have built a mock object using EasyMock, and I'm trying to have the
I have built a site using php and want to try keep it one
I have built ICS source using make -j4, then I have modified the Music
In my iPhone app, I am using the in-built Git repository of Xcode 4
I have tested a web application I have built using MVC 2 on local
I have a WAR file that was built using grails framework and OSGi plugin.
Background: the current text editor i am using does not have built-in support for
I am using Eclipse with m2e. I have one ProjectA that is built as
I have built a site which requires user to login. Now I have decided

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.