博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linq To Entity多条件or查询处理
阅读量:4289 次
发布时间:2019-05-27

本文共 1926 字,大约阅读时间需要 6 分钟。

1.多条件OR使用实例:

//学费if (string.IsNullOrEmpty(tuition) == false && tuition != "0"){    Expression
> orQuery = q => false; foreach (var item in tuitionID) { Tuition tui = Tuition.GetMode(item); orQuery = orQuery.Or(q => q.TuitionFee >= tui.MinValue && q.TuitionFee < tui.MaxValue); } query = query.Where(orQuery);}

2.添加扩展方法

public static class Utility    {        public static Expression
Compose
(this Expression
first, Expression
second, Func
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
(merge(first.Body, secondBody), first.Parameters); } public static Expression
> And
(this Expression
> first, Expression
> second) { return first.Compose(second, Expression.And); } public static Expression
> Or
(this Expression
> first, Expression
> second) { return first.Compose(second, Expression.Or); } } public class ParameterRebinder : ExpressionVisitor { private readonly Dictionary
map; public ParameterRebinder(Dictionary
map) { this.map = map ?? new Dictionary
(); } public static Expression ReplaceParameters(Dictionary
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); } }

更多参考:

转载地址:http://duogi.baihongyu.com/

你可能感兴趣的文章
【利用TableView实现QQ好友列表】
查看>>
【农民要翻身电脑主题 热门主题】
查看>>
【delphi程序设计改进可读性一法】
查看>>
【详解Windows7系统审核监控功能】
查看>>
【用Win7备份与还原让系统轻松回魂】
查看>>
【用Win7自带的DVD Maker制作视频】
查看>>
【幸福相守Windows7桌面热门主题】
查看>>
【天天酷跑新手全攻略】
查看>>
【有道云笔记手机PC端自动同步】
查看>>
【黑色蕾丝性感女神XP电脑主题】
查看>>
【XP系统命令提示符都有哪些运用技巧】
查看>>
【4个Win7系统轻松清理记录小技巧】
查看>>
【页面脚本错误快速解决方案】
查看>>
【php输出内容乱码解决方法】
查看>>
【Win8两种安全模式互相切换图形说明】
查看>>
【Win7系统去掉磁盘保护教程】
查看>>
【炫酷战机Win7热门主题】
查看>>
【可爱接吻鱼电脑主题】
查看>>
【详细介绍常见XP系统的配置要求】
查看>>
【解决使用scp命令时不必输入密码的程序】
查看>>