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

  • Home
  • SEARCH
  • 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 1005755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:20:53+00:00 2026-05-16T08:20:53+00:00

How can I build an expression tree when parts of the expression are passed

  • 0

How can I build an expression tree when parts of the expression are passed as arguments?

E.g. what if I wanted to create expression trees like these:

IQueryable<LxUser> test1(IQueryable<LxUser> query, string foo, string bar)
{
  query=query.Where(x => x.Foo.StartsWith(foo));
  return query.Where(x => x.Bar.StartsWith(bar));
}

but by creating them indirectly:

IQueryable<LxUser> test2(IQueryable<LxUser> query, string foo, string bar)
{
  query=testAdd(query, x => x.Foo, foo);
  return testAdd(query, x => x.Bar, bar);
}

IQueryable<T> testAdd<T>(IQueryable<T> query, 
  Expression<Func<T, string>> select, string find)
{
  // how can I combine the select expression with StartsWith?
  return query.Where(x => select(x) .. y => y.StartsWith(find));
}

Result:

While the samples didn’t make much sense (sorry but I was trying to keep it simple), here’s the result (thanks Quartermeister).

It can be used with Linq-to-Sql to search for a string that starts-with or is equal to the findText.

public static IQueryable<T> WhereLikeOrExact<T>(IQueryable<T> query, 
  Expression<Func<T, string>> selectField, string findText)
{
  Expression<Func<string, bool>> find;
  if (string.IsNullOrEmpty(findText) || findText=="*") return query;

  if (findText.EndsWith("*")) 
    find=x => x.StartsWith(findText.Substring(0, findText.Length-1));
  else
    find=x => x==findText;

  var p=Expression.Parameter(typeof(T), null);
  var xpr=Expression.Invoke(find, Expression.Invoke(selectField, p));

  return query.Where(Expression.Lambda<Func<T, bool>>(xpr, p));
}

e.g.

var query=context.User;

query=WhereLikeOrExact(query, x => x.FirstName, find.FirstName);
query=WhereLikeOrExact(query, x => x.LastName, find.LastName);
  • 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-16T08:20:53+00:00Added an answer on May 16, 2026 at 8:20 am

    You can use Expression.Invoke to create an expression that represents applying one expression to another, and Expression.Lambda to create a new lambda expression for the combined expression. Something like this:

    IQueryable<T> testAdd<T>(IQueryable<T> query, 
        Expression<Func<T, string>> select, string find)
    {
        Expression<Func<string, bool>> startsWith = y => y.StartsWith(find);
        var parameter = Expression.Parameter(typeof(T), null);
        return query.Where(
            Expression.Lambda<Func<T, bool>>(
                Expression.Invoke(
                    startsWith,
                    Expression.Invoke(select, parameter)),
                parameter));
    }
    

    The inner Expression.Invoke represents the expression select(x) and the outer one represents calling y => y.StartsWith(find) on the value returned by select(x).

    You could also use Expression.Call to represent the call to StartsWith without using a second lambda:

    IQueryable<T> testAdd<T>(IQueryable<T> query,
        Expression<Func<T, string>> select, string find)
    {
        var parameter = Expression.Parameter(typeof(T), null);
        return query.Where(
            Expression.Lambda<Func<T, bool>>(
                Expression.Call(
                    Expression.Invoke(select, parameter),
                    "StartsWith",
                    null,
                    Expression.Constant(find)),
                parameter));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to dynamically build up an expression tree so that I can
I'm trying to build an expression tree for this linq query: so I can
Given a expression like 4 * 5 + 9, how can we build a
How can I build a regular expression in python which can match all the
I am trying to build an expression tree programmatically. I have in my input,
I need to build the tree representing a Java regular expression. java.util.regex.Pattern do the
I wonder how I can build a query expression which understands the given item
I have build expression parser CreateExpression() which return constructed Expression Tree Expression rule =
I need to build ONE regular expression that can detect ALL the following strings
I am currently looking for a way where I can build a lambda expression

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.