我如何扩展这个表达式?

时间:2011-07-09 15:42:43

标签: c# linq expression-trees

我把这段代码作为一个例子,基本上吐出来了

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

但是我会扩展这个以做这样的事情:

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

以下是我为自己需要重构的代码示例:

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

为了进一步解释,让我告诉你我想做什么:

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;}
}

然后使用表达式构建如下:

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

或者

p => p.Loans.Person.IdentityCode.StartsWith("123")

2 个答案:

答案 0 :(得分:2)

未经测试,但......

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

MemberExpression memberExp = 
    Expression.MakeMemberAccess(paramExp, t.GetMember(propertyName).FirstOrDefault());

会变成:

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

MemberExpression otherEntityExp = 
    Expression.MakeMemberAccess(paramExp, t.GetMember("anotherentity").FirstOrDefault());

MemberExpression memberExp = 
    Expression.MakeMemberAccess(otherEntityExp, t.GetMember(propertyName).FirstOrDefault());

答案 1 :(得分:0)

我不确定你要求的是什么,更新表达式或从头开始构建一个......

如果您已经拥有现有的旧表达式并想要更新它,那么创建一个新表达式将非常容易。我们的想法是将表达式树挖掘到要替换的表达式。然后使用新替换的表达式更新所有父表达式。

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

否则要构建表达式树:

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

从头开始练习。

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);

我会留下最后一个作为练习。