因需求忒多,所以自己拼接了下备忘。
/// <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();
原文地址:https://www.cnblogs.com/OleRookie/p/8645725.html
时间: 2024-10-16 11:35:15