如何为EF表达式加入多个条件 [英] How to join multiple conditions for EF Expressions
本文介绍了如何为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,您需要来自
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表达式加入多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文