首页 新闻 赞助 找找看

linq where 泛型集合 对比自身model的条件查询

0
悬赏园豆:100 [待解决问题]

查询的类 TRequest (有分页的属性,和泛型 model)

    public class Request : ModelBase
    {
        public Request()
        {
            PageSize = 5000;
        }
        public int Top
        {
            set
            {
                this.PageSize = value;
                this.PageIndex = 1;
            }
        }

        public int PageSize { get; set; }
        public int PageIndex { get; set; }
    }



    public class TRequest<TModel> : Request where TModel : ModelBase
    {
        public TModel QuestModel;

    }

 

增删改 都可以用泛型 实现 , 查询的方法要根据每个model的属性查询,不知道怎么用linq实现了。

        /// <summary>
        /// 获取model list
        /// </summary>
        /// <typeparam name="TModel">实体model</typeparam>
        /// <param name="request"> 查询的条件</param>
        /// <returns></returns>
        public IEnumerable<TModel> GetList<TModel>(TRequest<TModel> request = null) where TModel : ModelBase
        {
            request = request ?? new TRequest<TModel>();
            using (var dbContext = new TOADbContext<TModel>())
            {
                IQueryable<TModel> models = dbContext.Models;

                //这里通过传过来的 mode 去匹配 model list 获取集合 (不知道怎么实现了)
                //if (!string.IsNullOrEmpty(request.QuestModel.Name))
                //    Mater = Mater.Where(u => u.Name.Contains(request.QuestModel.Name));



                return models.OrderByDescending(u => u.ID).ToPagedList(request.PageIndex, request.PageSize);
            }
        }

 

 

通过反射 去获取属性,貌似很麻烦。

 

怎么才能通过linq 去实现泛型的查询,谢谢!

yufeiyunsui的主页 yufeiyunsui | 初学一级 | 园豆:102
提问于:2015-11-13 15:56
< >
分享
所有回答(3)
0

难在条件语句生成规则的制定上。你制定一个条件规则,比如实现 一个类似mongodb客户端那样的规约模式。

或者像linq那样解析拉姆达表达式 

吴瑞祥 | 园豆:29449 (高人七级) | 2015-11-13 16:23
0

这里的TRequest<TModel>应该是Predicate<TModel>吧

jello chen | 园豆:7306 (大侠五级) | 2015-11-13 16:53

带分页属性的 泛型model  用来查询的。

public class TRequest<TModel> : Request where TModel : ModelBase

{ public TModel QuestModel; }

支持(0) 反对(0) yufeiyunsui | 园豆:102 (初学一级) | 2015-11-18 10:10

@yufeiyunsui: 那就生成一个Predicate<T>,例如这样:

static class Extensions
    {
        /// <summary>
        /// 实体转为谓语表达式
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public static Expression<Func<TModel, bool>> ToPredicate<TModel>(this TModel model)
        {
            if (model == null)
                throw new ArgumentNullException("model");
            var parameter = Expression.Parameter(typeof (TModel));
            var predicateExpr = Expression.Equal(Expression.Constant(1), Expression.Constant(1));
            var type = model.GetType();
            var props = type.GetProperties();
            foreach (var propertyInfo in props)
            {
                var propValue = propertyInfo.GetValue(model);
                var defaultValue = propertyInfo.PropertyType.GetDefaultValue();
                if (!object.Equals(propValue, defaultValue))
                {
                    predicateExpr = Expression.And(predicateExpr,
                        Expression.Equal(Expression.Property(parameter, propertyInfo.Name),
                            Expression.Constant(propValue)));
                }
            }
            return Expression.Lambda<Func<TModel, bool>>(predicateExpr, parameter);
        }
        /// <summary>
        /// 获取默认值
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private static object GetDefaultValue(this Type type)
        {
            if (type == null)
                throw new ArgumentNullException("type");
            return type.IsValueType ? Activator.CreateInstance(type) : null;
        }
    }
View Code
支持(0) 反对(0) jello chen | 园豆:7306 (大侠五级) | 2015-11-18 20:20

@jello chen: 不好意思  我生成的时候报错,

var propValue = propertyInfo.GetValue(model);

var defaultValue = propertyInfo.PropertyType.GetDefaultValue();

“System.Reflection.PropertyInfo”不包含“GetValue”的定义,并且最佳扩展方法重载“EntityFramework.Extensions.DataRecordExtensions.GetValue(System.Data.IDataRecord, string)”的某些参数无效

可以加点注释吗 ?   不是太明白。谢谢了

 

支持(0) 反对(0) yufeiyunsui | 园豆:102 (初学一级) | 2015-11-23 13:46

@yufeiyunsui: GetValue是PropertyInfo的方法,大概的意思就是根据实体对象生成拉姆达表达式

支持(0) 反对(0) jello chen | 园豆:7306 (大侠五级) | 2015-11-27 10:11
0

models.each(m=>)  用这个去实现

三人之行,必有我师 | 园豆:291 (菜鸟二级) | 2015-11-21 15:40

泛型貌似没法直接这么写

支持(0) 反对(0) yufeiyunsui | 园豆:102 (初学一级) | 2015-11-23 13:46
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册