创建predicate嵌套类与前pression [英] Create predicate with nested classes with Expression
问题描述
我有这样的:
公共类公司
{
公众诠释编号{获得;组; }
公共字符串名称{;组; }
}
公共类城市
{
公众诠释编号{获得;组; }
公共字符串名称{;组; }
公众诠释邮编code {获得;组; }
}
公共类Person
{
公众诠释编号{获得;组; }
公共字符串名字{获得;组; }
公共字符串名字{获得;组; }
公众诠释?年龄{获得;组; }
公共市市{获得;组; }
上市公司公司{获得;组; }
}
我想要一些情况下产生predicate是这样的:
VAR的结果= listPerson.Where(X => x.Age == 10).ToList<>();
或者这样:
VAR的结果= listPerson.Where(X => x.Company.Name == 1234).ToList();
或者这样:
VAR的结果= listPerson.Where(X => x.City.Zip code ==MyZip code)了ToList()。
或者这样:
VAR的结果= listPerson.Where(X => x.Company.Name ==MyCompanyName)了ToList()。
然后,我创建了一个predicateBuilder,这是工作(我得到的类型,如果为空的或不和我建立了predicate)当我这样做:
构建predicate<人>(时代,10);我得到这个:X => x.Age == 10
但我不如何管理时,有一个嵌套的属性是这样的:
构建predicate<人>(City.Zip code,MyZip code);
我想要得到这样的:X => x.City.Zip code ==MyZip code
或者这样:
构建predicate<人>(City.Name,MYNAME);
我想要得到这样的:X => x.City.Name ==MYNAME
或者这样:
构建predicate<人>(Company.Name,MyCompanyName);
我想要得到这样的:X => x.Company.Name ==MyCompanyName
(不打算重复乔恩 - OP与我联系,以提供一个答案)
下面似乎做工精细:
静态防爆pression< Func键< T,布尔>>建立predicate< T>(string成员,对象的值){
VAR P =前pression.Parameter(typeof运算(T));
防爆pression体= P;
的foreach(在member.Split变种亚段('。')){
身体=前pression.PropertyOrField(体,亚段);
}
返回前pression.Lambda< Func键< T,布尔>>(出pression.Equal(
身体,防爆pression.Constant(值,body.Type)),P);
}
这和乔恩的答案之间的唯一不同在于:它处理空
的略的更好,告诉防爆pression.Constant
什么是预期的类型。由于使用的演示:
静态无效的主要(){
VAR preD =生成predicate<人>(City.Name,MyCity);
变种人=新的[] {
新的Person {=城市新城区{名称=到其他地方}},
新的Person {=城市新城区{名称=MyCity}},
};
变种人= people.AsQueryable()单(preD)。
}
I have this :
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
}
public class City
{
public int Id { get; set; }
public string Name { get; set; }
public int ZipCode { get; set; }
}
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int? Age { get; set; }
public City City { get; set; }
public Company Company { get; set; }
}
I'd like a some case generate the predicate like this :
var result = listPerson.Where(x => x.Age == 10).ToList<>();
Or this :
var result = listPerson.Where( x => x.Company.Name == 1234).ToList();
Or this :
var result = listPerson.Where( x => x.City.ZipCode == "MyZipCode").ToList();
Or this :
var result = listPerson.Where( x => x.Company.Name == "MyCompanyName").ToList();
Then I created a "PredicateBuilder", that's work (I get the type, if nullable or not and I build the predicate) when I do this :
BuildPredicate<Person>("Age", 10); I get this : x => x.Age == 10
But I don't how manage when there is an nested property like this :
BuildPredicate<Person>("City.ZipCode", "MyZipCode");
I'd like get this : x => x.City.ZipCode == "MyZipCode"
Or this :
BuildPredicate<Person>("City.Name", "MyName");
I'd like get this : x => x.City.Name == "MyName"
Or this :
BuildPredicate<Person>("Company.Name", "MyCompanyName");
I'd like get this : x => x.Company.Name == "MyCompanyName"
(not intending to duplicate Jon - OP contacted me to provide an answer)
The following seems to work fine:
static Expression<Func<T,bool>> BuildPredicate<T>(string member, object value) {
var p = Expression.Parameter(typeof(T));
Expression body = p;
foreach (var subMember in member.Split('.')) {
body = Expression.PropertyOrField(body, subMember);
}
return Expression.Lambda<Func<T, bool>>(Expression.Equal(
body, Expression.Constant(value, body.Type)), p);
}
The only functional difference between that and Jon's answer is that it handles null
slightly better, by telling Expression.Constant
what the expected type is. As a demonstration of usage:
static void Main() {
var pred = BuildPredicate<Person>("City.Name", "MyCity");
var people = new[] {
new Person { City = new City { Name = "Somewhere Else"} },
new Person { City = new City { Name = "MyCity"} },
};
var person = people.AsQueryable().Single(pred);
}
这篇关于创建predicate嵌套类与前pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!