EF使用反射进行 Lambda 多条件动态拼接

下面是代码,其实主要就是用Expression生成指定的lambda,
算是抛砖引玉吧

	/// <summary>
    /// 多条件查询
    /// </summary>
    /// <typeparam name="TEntity">映射实体</typeparam>
    public class IQueryableForSearch<TEntity> where TEntity : TEntityFather
    {
        /// <summary>
        /// 通过反射实体和查询模型动态拼接查询条件
        /// </summary>
        /// <typeparam name="T">条件模型</typeparam>
        public static IQueryable<TEntity> GetIQueryable<T>(DbSet<TEntity> tEntity, T t)
        {
            IQueryable<TEntity> q1 = tEntity;
            if (t == null) return q1;
            var tPros = typeof(T).GetProperties();
            var tEntityPros = typeof(TEntity).GetProperties();
            foreach (var tPro in tPros)
            {
                foreach (var tEntityPro in tEntityPros)
                {
                    var tProVal = tPro.GetValue(t);
                    var tProType = tPro.PropertyType;
                    if (tPro.Name.Equals(tEntityPro.Name))
                    {
                        if ((tProType == typeof(string) && !string.IsNullOrEmpty((string)tProVal))
                            || (tProType.IsEnum && (byte)tProVal != 0))
                            q1 = q1.Where(PropertyEquals(tEntityPro, tProVal));
                        if (tEntityPro.PropertyType == typeof(DateTime?) && tProType == typeof(Search_DateTime))
                        {
                            var sdt = (Search_DateTime)tProVal;
                            if (sdt != null)
                            {
                                if (sdt.start != null)
                                    q1 = q1.Where(PropertyGreaterThanOrEqual<object>(tEntityPro, sdt.start));
                                if (sdt.end != null)
                                    q1 = q1.Where(PropertyLessThanOrEqual<object>(tEntityPro, sdt.end));
                            }
                        }
                    }
                }
            }
            //System.Diagnostics.Trace.WriteLine(q1.ToString());
            return q1;
        }
        /// <summary>
        /// 获取是否相等的lambda
        /// </summary>
        /// <typeparam name="TValue"></typeparam>
        public static Expression<Func<TEntity, bool>> PropertyEquals<TValue>(PropertyInfo property, TValue value)
        {
            var param = Expression.Parameter(typeof(TEntity));
            var body = Expression.Equal(Expression.Property(param, property), Expression.Constant(value));
            return Expression.Lambda<Func<TEntity, bool>>(body, param);
        }
        /// <summary>
        /// 时间点大于等于
        /// </summary>
        public static Expression<Func<TEntity, bool>> PropertyGreaterThanOrEqual<TValue>(PropertyInfo property, TValue value)
        {
            var param = Expression.Parameter(typeof(TEntity));
            var body = Expression.GreaterThanOrEqual(Expression.Property(param, property), Expression.Constant(value, typeof(DateTime?)));
            return Expression.Lambda<Func<TEntity, bool>>(body, param);
        }
        /// <summary>
        /// 时间点小于等于
        /// </summary>
        public static Expression<Func<TEntity, bool>> PropertyLessThanOrEqual<TValue>(PropertyInfo property, TValue value)
        {
            var param = Expression.Parameter(typeof(TEntity));
            var body = Expression.LessThanOrEqual(Expression.Property(param, property), Expression.Constant(value, typeof(DateTime?)));
            return Expression.Lambda<Func<TEntity, bool>>(body, param);
        }

        /// <summary>
        /// 多条件查询
        /// </summary>
        /// <param name="tEntity">映射实体</param>
        /// <param name="objs">查询条件列表</param>
        /// <returns></returns>
        public static IQueryable<TEntity> GetIQueryable(DbSet<TEntity> tEntity, params object[] objs)
        {
            IQueryable<TEntity> q1 = tEntity;
            var tEntityPros = typeof(TEntity).GetProperties();
            foreach (var obj in objs)
            {
                foreach (var tEntityPro in tEntityPros)
                {
                    var objType = obj.GetType();
                    if (objType.Name.Equals(tEntityPro.Name))
                    {
                        if ((objType == typeof(string) && !string.IsNullOrEmpty((string)obj))
                            || (objType.IsEnum && (byte)obj != 0))
                            q1 = q1.Where(PropertyEquals(tEntityPro, obj));
                        if (tEntityPro.PropertyType == typeof(DateTime?) && objType == typeof(Search_DateTime))
                        {
                            var sdt = (Search_DateTime)obj;
                            if (sdt != null)
                            {
                                if (sdt.start != null)
                                    q1 = q1.Where(PropertyGreaterThanOrEqual<object>(tEntityPro, sdt.start));
                                if (sdt.end != null)
                                    q1 = q1.Where(PropertyLessThanOrEqual<object>(tEntityPro, sdt.end));
                            }
                        }
                    }
                }
            }
            //System.Diagnostics.Trace.WriteLine(q1.ToString());
            return q1;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值