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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:09:36+00:00 2026-05-20T10:09:36+00:00

I trying to append where predicates and my goal is to create the same

  • 0

I trying to append where predicates and my goal is to create the same expression as:

Services.Where(s => s.Name == "Modules" && s.Namespace == "Namespace");

I have the following code:

Expression<Func<Service,string>> sel1 = s => s.Name;
Expression<Func<Service,string>> sel2 = s => s.Namespace;

var val1 = Expression.Constant("Modules");
var val2 = Expression.Constant("Namespace");

Expression e1 = Expression.Equal(sel1.Body, val1);
Expression e2 = Expression.Equal(sel2.Body, val2);
var andExp = Expression.AndAlso(e1, e2);

ParameterExpression argParam = Expression.Parameter(typeof(string), "s");
var lambda = Expression.Lambda<Func<string, bool>>(andExp, argParam);

This create the following output:

s => ((s.Name == "Modules") AndAlso (s.Namespace == "Namespace"))

However, this is faulty since the parameter for Name and Namespace isn’t the same. If I change one of the expression selector to:

Expression<Func<Service,string>> sel2 = srv => srv.Namespace;

The output will be:

s => ((s.Name == "Modules") AndAlso (srv.Namespace == "Namespace"))

How can I create a valid expression with use of sel1 and sel2?

UPDATE (28 feb 2011)

I solved it by creating invoke expressions: Expression.Invoke so the lambda expressions sel1 and sel2 don’t necessary need to be a MemberExpression:

Expression<Func<Service,string>> sel1 = s => s.Name;
Expression<Func<Service,string>> sel2 = srv => srv.Namespace;

var val1 = Expression.Constant("Modules");
var val2 = Expression.Constant("Namespace");

Expression<Func<Service, bool>> lambda = m => true;
var modelParameter = lambda.Parameters.First();

// sel1 predicate
{
    var invokedExpr = Expression.Invoke(sel1, modelParameter);
    var binaryExpression = Expression.Equal(invokedExpr, val1);
    lambda = Expression.Lambda<Func<Service, bool>>(Expression.AndAlso(binaryExpression, lambda.Body), lambda.Parameters);
}
// sel2 predicate
{
    var invokedExpr = Expression.Invoke(sel2, modelParameter);
    var binaryExpression = Expression.Equal(invokedExpr, val2);
    lambda = Expression.Lambda<Func<Service, bool>>(Expression.AndAlso(binaryExpression, lambda.Body), lambda.Parameters);
}
  • 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-20T10:09:37+00:00Added an answer on May 20, 2026 at 10:09 am

    It’s hard to mix compiler-generated expression trees and hand-made ones, precisely because of this sort of thing – extracting out the ParameterExpressions is tricky. So let’s start from scratch:

    ParameterExpression argParam = Expression.Parameter(typeof(Service), "s");
    Expression nameProperty = Expression.Property(argParam, "Name");
    Expression namespaceProperty = Expression.Property(argParam, "Namespace");
    
    var val1 = Expression.Constant("Modules");
    var val2 = Expression.Constant("Namespace");
    
    Expression e1 = Expression.Equal(nameProperty, val1);
    Expression e2 = Expression.Equal(namespaceProperty, val2);
    var andExp = Expression.AndAlso(e1, e2);
    
    var lambda = Expression.Lambda<Func<Service, bool>>(andExp, argParam);
    

    One important aspect I’ve changed is the type passed to Expression.Parameter – it certainly looks like it should be a Service rather than a string.

    I’ve given that a try, and it seemed to work when I called lambda.Compile and executed it on a couple of sample Service objects…

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

Sidebar

Related Questions

After trying to append some code to a div layer I received the following
I am trying to append a table cell using the following javascript function: function
I am trying to append date to the file name before copying in PHP.
I'm trying to append the following table built up in a string to a
I have a simple input field : <input type=text id=someid name=somename class=someclass> I'm trying
Im trying to append some JSON data from the last.fm API, I have been
Im trying to append some JSON data from the last.fm API, I have been
I'm trying to append two JavaScript files to all pages in JIRA, following this
I have been trying to append two 2 D arrays in java. Is it
I am trying to append the name of file in the first line of

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.