原方法:

public ProductDC[] GetProductQueryCondition(ProductQueryConditionDC filter)
{//红色为原来的联合条件(之后下面的修改过的方法是使用了,相关的条件联合方法)

var datas = Repository.LoadEntities<Product>(o => o.Status != (int)ProductStatus.Disabled
&& ((!string.IsNullOrEmpty(filter.ProductNo) && (o.ProductNo.Contains(filter.ProductNo)))
|| (!string.IsNullOrEmpty(filter.ProductName) && (o.ProductNo.Contains(filter.ProductName)))
|| ((filter.Category == (int)ProductCategory.All ? (new int?[] { (int)ProductCategory.FinishedProduct, (int)ProductCategory.SemiFinishedProduct }).Contains(o.Category) : o.Category == filter.Category))
|| ((filter.Status == (int)ProductStatus.All ? (new int?[] { (int)ProductStatus.Building, (int)ProductStatus.SignIn, (int)ProductStatus.TobeReleased, (int)ProductStatus.Released }).Contains(o.Status) : o.Status == filter.Status))
|| (!string.IsNullOrEmpty(filter.Customer) && (o.ProductNo.Contains(filter.Customer)))
|| ((filter.StartDate != null ? (filter.EndDate != null ? (o.MkDate > filter.StartDate && o.MkDate < filter.EndDate) : o.MkDate > filter.StartDate) : (filter.EndDate != null ? o.MkDate < filter.EndDate : o.MkDate == o.MkDate)))))
.Select(p => new ProductDC()
{
ProductId = p.ProductId,
ProductNo = p.ProductNo,
ProductName = p.ProductName,
Ver = p.Ver,
Spec = p.Spec,
Customer = p.Customer,
Category = p.Category,
CategoryName = p.Category == (int)ProductCategory.FinishedProduct ? "成品" : p.Category == (int)ProductCategory.SemiFinishedProduct ? "半成品" : "",
Status = p.Status,
StatusName = p.Status == (int)ProductStatus.Building ? "新建中" : p.Status == (int)ProductStatus.SignIn ? "会签中" : p.Status == (int)ProductStatus.TobeReleased ? "待发布" : p.Status == (int)ProductStatus.Released ? "已发布" : p.Status == (int)ProductStatus.Disabled ? "已停用" : "",
Description = p.Description,
Other1 = p.Other1,
Other2 = p.Other2,
Other3 = p.Other3,
Other4 = p.Other4,
MkUserId = p.MkUserId,
MkDate = p.MkDate,
CountersignCode = p.CountersignCode,
Pubdate = p.Pubdate,
Stopdate = p.Stopdate
});
return datas.ToArray();}

方法:

public ProductDC[] GetProductQueryCondition(ProductQueryConditionDC filter)
{

var Predicate = PredicateBuilder.True<Product>();//PredicateBuilder是下面红色标记的条件联合类

Predicate = Predicate.And<Product>(x => x.Status != (int)ProductStatus.Disabled);
if (!string.IsNullOrWhiteSpace(filter.ProductNo))
{
Predicate = Predicate.And<Product>(x => x.ProductNo.Contains(filter.ProductNo));
}
if (!string.IsNullOrWhiteSpace(filter.ProductName))
{
Predicate = Predicate.And<Product>(x => x.ProductName.Contains(filter.ProductName));
}
if (filter.Category == (int)ProductCategory.All)
{
Predicate = Predicate.And<Product>(x => new int?[] { (int)ProductCategory.FinishedProduct, (int)ProductCategory.SemiFinishedProduct }.Contains(x.Category));
}
else
{
Predicate = Predicate.And<Product>(x => x.Category == filter.Category);
}
if (filter.Status == (int)ProductStatus.All)
{
Predicate = Predicate.And<Product>(x => new int?[] { (int)ProductStatus.Building, (int)ProductStatus.SignIn, (int)ProductStatus.TobeReleased, (int)ProductStatus.Released }.Contains(x.Status));
}
else
{
Predicate = Predicate.And<Product>(x => x.Status == filter.Status);
}
if (!string.IsNullOrWhiteSpace(filter.Customer))
{
Predicate = Predicate.And<Product>(x => x.Customer.Contains(filter.Customer));
}
if (filter.StartDate.HasValue)
{
Predicate = Predicate.And<Product>(x => x.MkDate > filter.StartDate.GetValueOrDefault());
}
if (filter.EndDate.HasValue)
{
Predicate = Predicate.And<Product>(x => x.MkDate < filter.EndDate.GetValueOrDefault());
}

var datas = Repository.LoadEntities<Product>(Predicate)
.Select(p => new ProductDC()
{
ProductId = p.ProductId,
ProductNo = p.ProductNo,
ProductName = p.ProductName,
Ver = p.Ver,
Spec = p.Spec,
Customer = p.Customer,
Category = p.Category,
CategoryName = p.Category == (int)ProductCategory.FinishedProduct ? "成品" : p.Category == (int)ProductCategory.SemiFinishedProduct ? "半成品" : "",
Status = p.Status,
StatusName = p.Status == (int)ProductStatus.Building ? "新建中" : p.Status == (int)ProductStatus.SignIn ? "会签中" : p.Status == (int)ProductStatus.TobeReleased ? "待发布" : p.Status == (int)ProductStatus.Released ? "已发布" : p.Status == (int)ProductStatus.Disabled ? "已停用" : "",
Description = p.Description,
Other1 = p.Other1,
Other2 = p.Other2,
Other3 = p.Other3,
Other4 = p.Other4,
MkUserId = p.MkUserId,
MkDate = p.MkDate,
CountersignCode = p.CountersignCode,
Pubdate = p.Pubdate,
Stopdate = p.Stopdate
});
return datas.ToArray();
}

使用的条件联合的类

#region PredicateBuilder
public static class PredicateBuilder
{

/// <summary>
/// 机关函数应用False时:单个AND无效,多个AND无效;单个OR有效,多个OR有效;混应时写在OR后面的AND有效
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static Expression<Func<T, bool>> True<T>() { return f => true; }

/// <summary>
/// 机关函数应用True时:单个AND有效,多个AND有效;单个OR无效,多个OR无效;混应时写在AND后的OR有效
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static Expression<Func<T, bool>> False<T>() { return f => false; }
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);
}
}
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);
}
}

Linq 联合条件查询快捷方法的更多相关文章

  1. Mybatis-技术专区-Criteria的and和or进行联合条件查询

    之前用Mybatis框架反向的实体,还有实体里面的Example,之前只是知道Example里面放的是条件查询的方法,可以一直不知道怎么用,到今天才开始知道怎么简单的用.在我们前台查询的时候会有许多的 ...

  2. linq 多条件查询

    Linq 进行多条件查询的时候使用PredicateBuilder帮助类可以很好的解决. 类的源码: public static class PredicateBuilder { /// <su ...

  3. Linq in条件查询

    Linq 实现sql中的not in和in条件查询   T-SQL的IN: Select ProductID, ProductName, CategoryID From dbo.Products Wh ...

  4. [C#] Linq 动态条件查询

    应用背景:以货品为例,在基础数据中配置货品的判断规则,要根据这个规则筛选出符合条件的集合. 创建货品类 public class Product { public string Name { get; ...

  5. EntityFramework linq 多条件查询,不定条件查询

    一.场景描述: 开发的时候,有些查询功能,往往查询的条件是不确定的,用户没有填的不参与到查询中去. 如图1所示,用户可能只要给根据名称来查询即可,有时候开始时间和结束时间并不需要填写. 图 1 二.解 ...

  6. linq 多条件查询 where 拼接+分页

    首先定义一个静态类 public static class QueryAssembly { /// <summary> /// 返回true /// </summary> // ...

  7. qt sql多重条件查询简便方法

    转载请注明出处:http://www.cnblogs.com/dachen408/p/7457312.html 程序设计过程中,经常要涉及到查询,并且有很多条件,且条件可为空,如果逐个判断,会有很多情 ...

  8. linq自定义条件Lambda过滤方法

    Public Func<NoramalClass,bool>simpleComare<NormalClass>(string property,object value) { ...

  9. 转 --简单解决Linq多条件组合问题

    本文笔者用清晰的实例,解决了Linq多条件问题,思路十分的清晰,笔者也很细心的做了描述,希望能给你带来帮助. 最近有个项目准备功能改版,师兄吩咐:尽可能地做到万般皆Linq,所以很多东西都要从存储过程 ...

随机推荐

  1. Drools 7.4.1.Final参考手册(十四)集成Spring

    集成Spring Drools 6.0重要变更 Drools Spring集成经历了与Drools 6.0的变化完全一致的改造. 以下是一些主要的变化: T*推荐的Drools Spring的前缀已经 ...

  2. 第十四次ScrumMeeting会议

    第十三次Scrum Meeting 时间:2017/12/2 地点:咖啡馆 人员:策划组美工组 目前工作情况 名字 完成的工作 计划工作 蔡帜 科技树策划设计,科技图鉴蓝图设计 员工方面细节设定 游心 ...

  3. ssh问题_2

    前一段时间配置hadoop集群环境,发现一个现象,教程中的命令形式是ssh hostname,当然这个hostname应该是在ssh发起者的hosts文件中和相应的IP对应:现在问题来了: 我用的是m ...

  4. PAT 1045 快速排序

    https://pintia.cn/problem-sets/994805260223102976/problems/994805278589960192 著名的快速排序算法里有一个经典的划分过程:我 ...

  5. Delphi 7学习开发控件(续)

    继上次我们学习开发一个简单的画线控件后,基本的制作控件步骤已经清楚了,这次我们继续加深学习控件的制作.我们打开Delphi 7创建一个应用程序,拖动LineTo控件到窗体上,仔细看左边的对象设计器,可 ...

  6. ZOJ 2314 Reactor Cooling | 无源汇可行流

    题目: 无源汇可行流例题 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 题解: 证明什么的就算了,下面给出一种建图方式 ...

  7. 洛谷 P2387 [NOI2014]魔法森林 解题报告

    P2387 [NOI2014]魔法森林 题目描述 为了得到书法大家的真传,小 E 同学下定决心去拜访住在魔法森林中的隐 士.魔法森林可以被看成一个包含 n 个节点 m 条边的无向图,节点标号为 1,2 ...

  8. HDU4280:Island Transport(最大流)

    Island Transport Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  9. idea设置文件的编码格式

    在打开某些类时会发现注释是乱码的,该如何解决idea的文件乱码呢?这就需要设置这个文件的合适编码格式: idea设置文件编码的两种方式分别如下: 第一种方式点击idea的右下角的图标如下图所示: 第二 ...

  10. AngularJs开发——控制器间的通信

    AngularJs开发——控制器间的通信 指令与控制器之间通信,无非是以下几种方法: 基于scope继承的方式 基于event传播的方式 service的方式 基于scope继承的方式 最简单的让控制 ...