NETLinQ的动态多条件查询
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
NETLinQ的动态多条件查询///<summary>
///重点
///构造函数使⽤True时:单个AND有效,多个AND有效;单个OR⽆效,多个OR⽆效;混合时写在AND后的OR有效
///构造函数使⽤False时:单个AND⽆效,多个AND⽆效;单个OR有效,多个OR有效;混合时写在OR后⾯的AND有效
///</summary>
public static class ExpressionExtensions
{
///<summary>
///以特定的条件运⾏组合两个Expression表达式
///</summary>
///<typeparam name="T">表达式的主实体类型</typeparam>
///<param name="first">第⼀个Expression表达式</param>
///<param name="second">要组合的Expression表达式</param>
///<param name="merge">组合条件运算⽅式</param>
///<returns>组合后的表达式</returns>
public static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second,
Func<Expression, Expression, Expression> merge)
{
var map =
first.Parameters.Select((f, i) => new { f, s = second.Parameters[i] }).ToDictionary(p => p.s, p => p.f);
var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
return mbda<T>(merge(first.Body, secondBody), first.Parameters);
}
///<summary>
///以 Expression.AndAlso 组合两个Expression表达式
///</summary>
///<typeparam name="T">表达式的主实体类型</typeparam>
///<param name="first">第⼀个Expression表达式</param>
///<param name="second">要组合的Expression表达式</param>
///<returns>组合后的表达式</returns>
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first,
Expression<Func<T, bool>> second)
{
return pose(second, Expression.AndAlso);
}
///<summary>
///传⼊条件之间为OR查询
///</summary>
///<typeparam name="T"></typeparam>
///<param name="source"></param>
///<param name="predicates"></param>
///<returns></returns>
public static IQueryable<T> WhereOR<T>(this IQueryable<T> source, params Expression<Func<T, bool>>[] predicates) {
if (source == null) throw new ArgumentNullException("source");
if (predicates == null) throw new ArgumentNullException("predicates");
if (predicates.Length == 0) return source.Where(x => false); // no matches!
if (predicates.Length == 1) return source.Where(predicates[0]); // simple
var param = Expression.Parameter(typeof(T), "x");
Expression body = Expression.Invoke(predicates[0], param);
for (int i = 1; i < predicates.Length; i++)
{
body = Expression.OrElse(body, Expression.Invoke(predicates[i], param));
}
var lambda = mbda<Func<T, bool>>(body, param);
return source.Where(lambda);
}
///<summary>
///以 Expression.OrElse 组合两个Expression表达式
///</summary>
///<typeparam name="T">表达式的主实体类型</typeparam>
///<param name="first">第⼀个Expression表达式</param>
///<param name="second">要组合的Expression表达式</param>
///<returns>组合后的表达式</returns>
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first,
Expression<Func<T, bool>> second)
{
return pose(second, Expression.Or);
}
public static Expression<Func<T, bool>> OrElse<T>(this Expression<Func<T, bool>> first,
Expression<Func<T, bool>> second)
{
return pose(second, Expression.OrElse);
}
private class ParameterRebinder : ExpressionVisitor
{
private readonly Dictionary<ParameterExpression, ParameterExpression> _map;
private 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 node)
{
ParameterExpression replacement;
if (_map.TryGetValue(node, out replacement))
node = replacement;
return base.VisitParameter(node);
}
}
}
使⽤
actionPlanList.Add(actionPlanModel);
Expression<Func<ActionPlanModel, bool>> ex = t => false;
if (projectNames.Count>0)
{
foreach (var projectName in projectNames)
{
ex = ex.Or(s => s.ProjectName==projectName);
}
}
if (coachAccountNames.Count>0)
{
foreach (var coachAccountName in coachAccountNames)
{
ex = ex.Or(s => s.CoachName==coachAccountName);
}
}
if (DealerNameCNs.Count>0)
{
foreach (var DealerNameCN in DealerNameCNs)
{
ex = ex.Or(s => s.DealerNameCN==DealerNameCN);
}
}
//And在后⾯
if (!string.IsNullOrEmpty(projectYear))
{
ex = ex.And(s => s.ProjectYear.Equals(projectYear));
}
actionPlanListNew = actionPlanList.Where(pile()).ToList();。