• Linq 联合条件查询快捷方法


    原方法:

    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);
    }
    }

  • 相关阅读:
    ios开发ios9新特性关键字学习:泛型,逆变,协变,__kindof
    ios开发Base64编码以及加密相关学习
    ios开发网络学习AFN三:AFN的序列化
    iOS开发AFN使用二:AFN文件下载与文件上传
    Android开发:《Gradle Recipes for Android》阅读笔记(翻译)2.4——更新新版本的Gradle
    Android开发:《Gradle Recipes for Android》阅读笔记(翻译)2.3——用Eclipse ADT导出App
    Android开发:《Gradle Recipes for Android》阅读笔记(翻译)2.2——将Eclipse开发的项目导入到AndroidStudio
    Android开发:《Gradle Recipes for Android》阅读笔记(翻译)2.1——设置项目参数
    Android开发:《Gradle Recipes for Android》阅读笔记1.7——仓库配置
    Android开发:《Gradle Recipes for Android》阅读笔记1.6——使用android studio添加依赖
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/7118086.html
Copyright © 2020-2023  润新知