本文共 1096 字,大约阅读时间需要 3 分钟。
static void Main(
string[] args)
{
List<
Person> list =
new List<
Person>() {
new Person(){ Age=20, Name=
"wengyuli", Parts=
new Part(){ Name=
"123"}},
new Person(){ Age=30, Name=
"chenyang", Parts=
new Part(){ Name=
"123456"}},
new Person(){ Age=40, Name=
"heshuai", Parts=
new Part(){ Name=
"123"}},
new Person(){ Age=50, Name=
"heyunlong", Parts=
new Part(){ Name=
"123456"}}
};
var c =
Expression.Parameter(
typeof(
Person),
"m");
Expression condition =
Expression.Constant(
false);
Expression con =
Expression.Call(
Expression.Property(c,
typeof(
Person).GetProperty(
"Name")),
typeof(
string).GetMethod(
"StartsWith",
new Type[]{
typeof(
string)}),
Expression.Constant(
"wengyuli"));
condition =
Expression.Or(con, condition);
Expression<
Func<
Person,
bool>> end =
Expression.Lambda<
Func<
Person,
bool>>(condition,
new ParameterExpression[] { c });
}
class Person {
public string Name {
set;
get; }
public int Age {
set;
get; }
public Part Parts {
set;
get; }
}
class Part {
public string Name {
set;
get; }
}
}
本文转自wengyuli 51CTO博客,原文链接:http://blog.51cto.com/wengyuli/586676,如需转载请自行联系原作者