动态 Linq/Lambda 过滤
我有一个返回表达式的方法,用于根据客户需求动态过滤记录,我在做这件事时遇到问题,我想要这样的东西,有
public Expression<T> FilterCreator<T>(FilterCondition condition, string columnName, object value)
{
Expression<Func<Customer, bool>> query;
// FilterCondition is an enum flag for conditions
if(condition.Condition == ConditionFlags.EQUALS)
{
// here is the problem,
// i want the emailAddress to be dynamic based on the passed columName parameter of the client
// and be able to cast its type of the value that was passed
query = p => p.EmailAddress == (typeof(p.EmailAddress))value;
//i want something like this
// query = p => p.(columnName)=> (typeOf(p.(columnName)))value;
}
else if(condition.Condition == ConditionFlags.CONTAINS)
{
.....
}
return query;
}
什么建议吗?提前致谢
I havea method that returns Expression to be used for dynamic filtering of records based on the clients needs, i have a problem in doing it, i want something like this
public Expression<T> FilterCreator<T>(FilterCondition condition, string columnName, object value)
{
Expression<Func<Customer, bool>> query;
// FilterCondition is an enum flag for conditions
if(condition.Condition == ConditionFlags.EQUALS)
{
// here is the problem,
// i want the emailAddress to be dynamic based on the passed columName parameter of the client
// and be able to cast its type of the value that was passed
query = p => p.EmailAddress == (typeof(p.EmailAddress))value;
//i want something like this
// query = p => p.(columnName)=> (typeOf(p.(columnName)))value;
}
else if(condition.Condition == ConditionFlags.CONTAINS)
{
.....
}
return query;
}
any advise guys? thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要构建一个表达式树:
You need to build an expression tree: