EF 公共查询类

Posted olerookie

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EF 公共查询类相关的知识,希望对你有一定的参考价值。

 

因需求忒多,所以自己拼接了下备忘。

/// <summary>
    /// 公共查询类
    /// </summary>
    public static class PublicPredicateBuilder
    {
        /// <summary>
        /// 地区组合查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="db"></param>
        /// <returns></returns>
        public static Expression<Func<W_Brjcxx, bool>> W_BrjcxxToArea(DB.BlContext db, Expression<Func<W_Brjcxx, bool>> predicate)
        {            
            if (RequestSession.GetSessionUser().UserAccount.ToString() != "admin")
            {
                string DeptNo = RequestSession.GetSessionUser().DeptNo.ToString().Replace("", "");
                var list_Base_Organization = db.Base_Organization.Where(d => DeptNo.Contains(d.Organization_ID)).ToList();
                foreach (var model_Base_Organization in list_Base_Organization)
                {
                    var predicate_Base_Organization = PredicateBuilder.True<W_Brjcxx>();
                    var areas = model_Base_Organization.AreaLV.Split(,).ToList();
                    var city = areas.Count >= 1 ? areas[0] : "";
                    var county = areas.Count >= 2 ? areas[1] : "";
                    var town = areas.Count >= 3 ? areas[2] : "";
                    var community = areas.Count >= 4 ? areas[3] : "";

                    int whereCount = 0;

                    if (!string.IsNullOrWhiteSpace(city) && city != "请选择")
                    {
                        predicate_Base_Organization = predicate_Base_Organization.And(d => d.AreaID.Contains(city));
                        whereCount++;
                    }
                    if (!string.IsNullOrWhiteSpace(county) && county != "请选择")
                    {
                        predicate_Base_Organization = predicate_Base_Organization.And(d => d.AreaID.Contains(county));
                        whereCount++;
                    }
                    if (!string.IsNullOrWhiteSpace(town) && county != "请选择")
                    {
                        predicate_Base_Organization = predicate_Base_Organization.And(d => d.AreaID.Contains(town));
                        whereCount++;
                    }
                    if (!string.IsNullOrWhiteSpace(community) && county != "请选择")
                    {
                        predicate_Base_Organization = predicate_Base_Organization.And(d => d.AreaID.Contains(community));
                        whereCount++;
                    }
                    if (whereCount == 0)
                    {
                        predicate_Base_Organization = predicate_Base_Organization.And(d => d.AreaID == "此人没有设置任何所属区域");
                    }

                    predicate = predicate.Or(predicate_Base_Organization);                    
                }


            }
            return predicate;
        }
    }

 

//例子

  var predicate = PredicateBuilder.True<W_Brjcxx>();
                if (!string.IsNullOrWhiteSpace(sname))
                {
                    predicate = predicate.And(d => d.XM.Contains(sname) || d.SF_number.Contains(sname));
                }
                if (!string.IsNullOrWhiteSpace(sarea))
                {
                    predicate = predicate.And(d => d.AreaID.Contains(sarea));
                }

                predicate = predicate.And(Model.PublicPredicateBuilder.W_BrjcxxToArea(db, predicate));

 var sql_W_Brjcxx = db.W_Brjcxx.Where(predicate).OrderBy(d => d.state).ThenByDescending(d => d.LR_time);
                var list_W_Brjcxx = sql_W_Brjcxx.Skip((PageControl1.PageIndex - 1) * PageControl1.PageSize).Take(PageControl1.PageSize)
                                                     .ToList();
  var predicate = PredicateBuilder.True<W_Brjcxx>();
                if (!string.IsNullOrWhiteSpace(sname))
                {
                    predicate = predicate.And(d => d.XM.Contains(sname) || d.SF_number.Contains(sname));
                }
                if (!string.IsNullOrWhiteSpace(sarea))
                {
                    predicate = predicate.And(d => d.AreaID.Contains(sarea));
                }
                //调用组合拼接
                predicate = predicate.And(Model.PublicPredicateBuilder.W_BrjcxxToArea(db, predicate));

 var sql_W_Brjcxx = db.W_Brjcxx.Where(predicate).OrderBy(d => d.state).ThenByDescending(d => d.LR_time);
                var list_W_Brjcxx = sql_W_Brjcxx.Skip((PageControl1.PageIndex - 1) * PageControl1.PageSize).Take(PageControl1.PageSize)
                                                     .ToList();

 

以上是关于EF 公共查询类的主要内容,如果未能解决你的问题,请参考以下文章

在android studio中升级repo v9后,片段必须是公共静态类崩溃错误

片段必须是公共静态类才能从实例状态正确重新创建

使用EF关于分页查询时遇到的一点疑问

为啥片段类应该是公开的?

.NET Entity Framework(EF)使用SqlQuery直接操作SQL查询语句或者执行过程

EF添加关联的提示问题:映射从第 260 行开始的片段时有问题: