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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:46:09+00:00 2026-05-23T16:46:09+00:00

I have this bit of code as an example, basically it spits out p

  • 0

I have this bit of code as an example, basically it spits out

p => p.fieldname.StartsWith("123")

But who would i expand on this to do something like this:

p => p.anotherentity.fieldname.StartsWith("123")

Here is a sample of the code i am have refactored for own needs:

string propertyName = "FirstName";
string methodName = "StartsWith";
string keyword = "123";

Type t = typeof (Person);

ParameterExpression paramExp = Expression.Parameter(t, "p");
// the parameter: p

MemberExpression memberExp = Expression.MakeMemberAccess(paramExp,
                                                         t.GetMember(propertyName).FirstOrDefault());
// part of the body: p.FirstName

MethodCallExpression callExp = Expression.Call(memberExp,
                                               typeof (string).GetMethod(methodName,
                                                                         new Type[] {typeof (string)}),
                                               Expression.Constant(keyword));
// the body: p.FirstName.StartsWith("123")

Expression<Func<Person, bool>> whereExp = Expression.Lambda<Func<Person, bool>>(callExp, paramExp);
Expression<Func<Person, string>> selectExp = Expression.Lambda<Func<Person, string>>(memberExp, paramExp);

Console.WriteLine(whereExp); // p => p.FirstName.StartsWith("123")
Console.WriteLine(selectExp); // p => p.FirstName

To further explain let me show you what i would like to do:

public class Person 
{
    public string IdentityCode {get;set;}
    public Loans Loans {get;set;}
}

public class Loans 
{
  public int Id {get;set;}
  public Asset Assets {get;set;}
  public Person person {get;set;}
}

public class Asset  
{
  public string SerialNumber {get;set;}
}

Then using an expression build something like this:

p => p.Loans.Asset.SerialNumber.StartsWith("123)

Or

p => p.Loans.Person.IdentityCode.StartsWith("123")
  • 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-23T16:46:09+00:00Added an answer on May 23, 2026 at 4:46 pm

    I’m not sure what you’re asking for, updating an expression or building one from scratch…

    If you already have the existing, old expression and want to update it, it would be very easy to create a new one. The idea is to dig through the expression tree down to the expression you want to replace. Then update all parent expressions with the newly replaced one.

    Expression<Func<Obj, bool>> expr = p => p.fieldname.StartsWith("123");
    var body = expr.Body as MethodCallExpression;   // *.StartsWith()
    var obj = body.Object as MemberExpression;      // p.fieldname
    var param = expr.Parameters.First();            // p
    var newAccess = Expression.PropertyOrField(param, "anotherentity"); // p.anotherentity
    var newObj = obj.Update(newAccess);                 // update obj
    var newBody = body.Update(newObj, body.Arguments);  // update body
    var newExpr = expr.Update(newBody, expr.Parameters);// update expr
    

    Otherwise to build up the expression tree:

    Expression<Func<Person, bool>> expr =
        p => p.Loans.Asset.SerialNumber.StartsWith("123");
    

    Work it out from the beginning.

    var p = Expression.Parameter(typeof(Person), "p");
    var accessLoans = Expression.PropertyOrField(p, "Loans");
    var accessAsset = Expression.PropertyOrField(accessLoans, "Asset");
    var accessSerialNumber = Expression.PropertyOrField(accessAsset, "SerialNumber");
    var callArgs = new Expression[] { Expression.Constant("123", typeof(string)) };
    var callStartsWith = Expression.Call(accessSerialNumber, "StartsWith", null, callArgs);
    var newExpr = Expression.Lambda<Func<Person, bool>>(callStartsWith, p);
    

    I’ll leave the last one as an exercise for you.

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

Sidebar

Related Questions

I have this bit of code I found on the web at the end
Well, I have this bit of code that is slowing down the program hugely
I use this bit of code to feed some data i have parsed from
its a little bit hard to understand. in the header.php i have this code:
I have a bit of code that looks like this: text = reg.Replace(text, new
I have the following bit of code, simply: $(function() { $('a.add-photos-link').live('click', function(e) { $(this).colorbox({
I have a bit of php code like this: $test = <!--my comment goes
I have problems with the following bit of javascript/jquery code: this.droppable = function(){ $('.imageWindow
I am having a bit of difficulty with this current code I have set
I know that this code has a bit of smell but it suits this

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.