如何为EF表达式加入多个条件

编程入门 行业动态 更新时间:2024-10-14 18:19:46
本文介绍了如何为EF表达式加入多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 Expression<Func<Dealer, bool>> GetFilter() { Expression<Func<Dealer, bool>> f = (d) => 1 == 1; var s = ""; if (QS["Province"] != null) { s = QS["Province"]; f = d => d.Address.Province.Equals(s); } if (QS["City"] != null) { s = QS["City"]; f = d => d.Address.City.Equals(s); } if (QS["NameStartWith"] != null) { s = QS["NameStartWith"]; f = d => d.DealerName.Substring(0, 1).ToUpper().Equals(s); } return f; }

我要根据查询字符串生成表达式,以便使用通用存储库从数据库中获取所有经销商.

Depending on querystrings I want to generate Expression for getting all the dealers from database using generic repository.

这是代码

Expression<Func<Dealer, bool>> filter = GetFilter(); GenericRepository<Dealer> DealerRepository = new GenericRepository<Dealer>(); List<Dealer> Dealers = DealerRepository.Get(filter).ToList();

推荐答案

我假设您使用此存储库模式?

问题是,您只有一个Expression变量(f),并且您正在覆盖其内容.因此查询字符串

The problem is, that you have only one Expression variable (f) and you are overwriting its contents. Thus the query string

Province = ONCA& City =多伦多

Province=ONCA&City=Toronto

将执行类似的操作

s = QS["Province"]; f = d => d.Address.Province.Equals(s); s = QS["City"]; f = d => d.Address.City.Equals(s);

但是f仅包含最后分配的值.要以EF友好的方式组合Lamda-Expression,您需要来自此MSDN帖子:

But f will only contain the last assigned value. To combine Lamda-Expressions in an EF-friendly way, you need some Utility Classes from this MSDN post:

public class ParameterRebinder : ExpressionVisitor { private readonly Dictionary<ParameterExpression, ParameterExpression> _map; public ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map) { _map = map ?? new Dictionary<ParameterExpression, ParameterExpression>(); } public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp) { return new ParameterRebinder(map).Visit(exp); } protected override Expression VisitParameter(ParameterExpression p) { ParameterExpression replacement; if (_map.TryGetValue(p, out replacement)) { p = replacement; } return base.VisitParameter(p); } } public static class Utility { public static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge) { // build parameter map (from parameters of second to parameters of first) var map = first.Parameters.Select((f, i) => new { f, s = second.Parameters[i] }).ToDictionary(p => p.s, p => p.f); // replace parameters in the second lambda expression with parameters from the first var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body); // apply composition of lambda expression bodies to parameters from the first expression return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters); } public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second) { return first.Compose(second, Expression.And); } public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second) { return first.Compose(second, Expression.Or); } }

现在您可以编写如下代码:

Now you can write code like this:

Expression<Func<string, bool>> filter = x => true; filter = filter.And(x => x.Contains("a")); filter = filter.And(x => x.Contains("b")); filter = filter.Or(x => x.Contains("c")); var compiled = filter.Compile(); Console.WriteLine(compiled.Invoke("aaa")); // False Console.WriteLine(compiled.Invoke("abba")); // True Console.WriteLine(compiled.Invoke("aac")); // True

或者您的情况:

Expression<Func<Dealer, bool>> GetFilter() { Expression<Func<Dealer, bool>> filter = x => true; if (QS["Province"] != null) { var s = QS["Province"]; filter = filter.And(d => d.Address.Province.Equals(s)); } if (QS["City"] != null) { var s = QS["City"]; filter = filter.And(d => d.Address.City.Equals(s)); } if (QS["NameStartWith"] != null) { var s = QS["NameStartWith"]; filter = filter.And(d => d.DealerName.Substring(0, 1).ToUpper().Equals(s)); } return filter; }

更多推荐

如何为EF表达式加入多个条件

本文发布于:2023-11-22 14:40:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617843.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   表达式   何为   条件   EF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!