(精华)2020年7月23日 C#基础知识点 表达式目录数实现组合继承(EF动态查询)
发布日期:2021-06-29 15:08:16
浏览次数:3
分类:技术文章
本文共 2683 字,大约阅读时间需要 8 分钟。
//定义数据模型类public class Base_UserTestDTO : Base_User{ public string DepartmentName { get; set; }}//即BaseBusiness中的Servicevar db = DbFactory.GetRepository();Expression> select = (a, b) => new Base_UserTestDTO{ DepartmentName = b.Name};select = select.BuildExtendSelectExpre();var q = from a in db.GetIQueryable ().AsExpandable() join b in db.GetIQueryable () on a.DepartmentId equals b.Id into ab from b in ab.DefaultIfEmpty() select @select.Invoke(a, b);//筛选var where = LinqHelper.True ();where = where.And(x => x.UserName == "Admin");//获取筛选数据var list = q.Where(where).ToList();
public static Expression> BuildExtendSelectExpre (this Expression > expression){ return GetExtendSelectExpre >(expression);}
private static ExpressionGetExtendSelectExpre (Expression expression){ NewExpression newBody = Expression.New(typeof(TResult)); MemberInitExpression oldExpression = (MemberInitExpression)expression.Body; ParameterExpression[] oldParamters = expression.Parameters.ToArray(); List existsProperties = new List (); oldExpression.Bindings.ToList().ForEach(aBinding => { existsProperties.Add(aBinding.Member.Name); }); List newBindings = new List (); typeof(TBase).GetProperties().Where(x => !existsProperties.Contains(x.Name)).ToList().ForEach(aProperty => { if (typeof(TResult).GetMembers().Any(x => x.Name == aProperty.Name)) { MemberBinding newMemberBinding = null; var valueExpre = Expression.Property(oldParamters[0], aProperty.Name); if (typeof(TBase).IsAssignableFrom(typeof(TResult))) { newMemberBinding = Expression.Bind(aProperty, valueExpre); } else { newMemberBinding = Expression.Bind(typeof(TResult).GetProperty(aProperty.Name), valueExpre); } newBindings.Add(newMemberBinding); } }); newBindings.AddRange(oldExpression.Bindings); var body = Expression.MemberInit(newBody, newBindings.ToArray()); var resExpression = Expression.Lambda (body, oldParamters); return resExpression;}
////// Linq操作帮助类 /// public static class LinqHelper { ////// 创建初始条件为True的表达式 /// ////// public static Expression > True () { return x => true; } /// /// 创建初始条件为False的表达式 /// ////// public static Expression > False () { return x => false; } }
转载地址:https://codeboy.blog.csdn.net/article/details/107539166 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2024年04月23日 13时45分01秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
tomcat配置JVM
2019-04-29
Oracle获取连接超级慢的问题
2019-04-29
关于HashMap初始化容量,设置多少合适。
2019-04-29
MYSQL 自定义函数
2019-04-29
早鸟票倒计时3天 | 2019携程技术峰会,11月9日上海(含福利)
2019-04-29
干货 | 微信小程序一键转百度小程序,携程火车票团队是这样做的
2019-04-29
沙龙回顾 | 移动互联网下的测试工程效率提升,含PPT和视频
2019-04-29
程里人 | 携程程序猿/媛的1024
2019-04-29
常见的字符集编码
2019-04-29
前端(一):字符与语义标签
2019-04-29
前端(二):CSS语法与选择器
2019-04-29
前端(三):样式继承与其他概念
2019-04-29
前端(四):样式继承与其他概念
2019-04-29
前端小案例:田径场
2019-04-29
前端(五):浮动
2019-04-29
前端(六):高度塌陷与BFC
2019-04-29
C++ 多线程
2019-04-29
C++ inline 内联函数
2019-04-29
动态规划问题汇总
2019-04-29
C++ 面向对象程序设计:数据抽象、继承、多态
2019-04-29