这里介绍LINQ+Lambda表达式进行数据筛选的方式
这里是第一种方式,还是使用了if条件语句去判断,根据选择的条件去筛选出我所需要的数据
public GxAnaly SelectDay(string ID, DateTime Day) { try { DBEntities db = new DBEntities(); var temp = (from pt in db.Tb_Analysis where pt.ID == Sn select pt).ToList(); // return temp.Any() ? LoadFromModel(temp.First()) : null; if (temp.Count == 0) { return null; } else { List<Analy> gxDetail = new List<Analy>(); List<Analy> Detail = new List<Analy>(); temp.ForEach(e => gxDetail.Add(LoadFromModel(e))); foreach (Analy Detail in gxDetail) { if (Detail.OtTime.Day == Day.Day&&Detail.OtTime.Month==Day.Month&&Detail.OtTime.Year==Day.Year) { Detail.Add(Detail); } } if (Detail.Count == 0) { return null; } else { return Detail.FirstOrDefault(); } } } catch (Exception ex) { return null; Logger.Error(ex.Message + ex.StackTrace); } }
第二种方式,直接用Lambda自带的筛选属性
using(var db=new MyDbContext()) { var s= db.Students.ToList().First(s=>s.ID=10086); } //这里写的是进行全表查询,不建议使用
//根据相应的条件进行查询,不会去查询全表,特别在数据量庞大的时候,建议加个判断using(var db=new MyDbContext()) { var list =db.Students.AsQueryable(); if(********){list=list.Where(s=>s.ID=1200);} if(******){list=list.Where(...)} }
下面展示一个多个Lambda表达式进行表操作的方法
public static class PredicateBuilder { public static Expression<Func<T, bool>> True<T>() { return f => true; } 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); }
完美的动态拼接Lambda表达式如下:
using (var db = new MyDbContext()) { var predicate = PredicateBuilder.True<Student>(); predicate=predicate.And(s => s.ID > 1200); predicate=predicate.Or(s => s.ID < 1000); var result = db.Students.Where(predicate).ToList(); }
下面是一种多Lambda表达式操作的一个范例,这里部分引用来自https://blog.csdn.net/leewhoee/article/details/8968023,自己懒得去敲了,顺手就借来用了
using (var db = new SHTrackerDbContext()) { var predicate = PredicateBuilder.True<Course>(); settings = DecorateSettings(settings); Expression<Func<Course, bool>> checkCourse = c => db.Students.Any(s => s.CourseID == c.ID); if (!string.IsNullOrEmpty(settings.Quater_Year)) { checkCourse = c => db.Students.Any(s => s.CourseID == c.ID && db.Student2CBOs.Any( s2c => s2c.StudentID == s.ID && s2c.Quater_Year.Equals(settings.Quater_Year))); } if (settings.QuaterYearArray != null) { checkCourse = c => db.Students.Any(s => s.CourseID == c.ID && db.Student2CBOs.Any( s2c => s2c.StudentID == s.ID && settings.QuaterYearArray.Any(qy => qy.Equals(s2c.Quater_Year)))); } if (!string.IsNullOrEmpty(settings.DPU_ID)) { checkCourse = checkCourse.And( c => db.Students.Any(s => s.CourseID == c.ID && s.DPU_ID.Equals(settings.DPU_ID))); } predicate = predicate.And(checkCourse); if (settings.IsCheckInstructorName) { predicate = predicate.And(c => c.InstructorName.Equals(settings.InstructorName)); } if (!string.IsNullOrEmpty(settings.Term)) { predicate = predicate.And(c => c.TermDescription.Equals(settings.Term)); } if (settings.TermArray != null) { predicate = predicate.And(c => settings.TermArray.Any(t => t.Equals(c.TermDescription))); } if (settings.CourseType != CourseType.All) { predicate = predicate.And(c => c.Type == (int) settings.CourseType); } var cc = new CourseCollection( db.Courses.AsNoTracking() .Where(predicate) .OrderByDescending(m => m.ID) .Skip((pageIndex - 1)*pageSize) .Take(pageSize) .ToList(), db.Courses.AsNoTracking().Where(predicate).Count()) { PageIndex = pageIndex, PageSize = pageSize, Settings = DecorateSettings(settings) }; return cc; }
原文地址:https://www.cnblogs.com/yinxuejunfeng/p/9095878.html
时间: 2024-10-12 10:38:18