首页 新闻 会员 周边

在项目中用EF的兄弟,你们如何处理动态查询这类问题

0
悬赏园豆:50 [已解决问题] 解决于 2015-07-14 18:43

好吧,我已经被这玩意折腾疯了,谁手头上有Expression查询装配器,能否分享一下?

支持条件连接(and,or 带括号分组的,带正序、逆序order的) 

羊汤大饼的主页 羊汤大饼 | 初学一级 | 园豆:109
提问于:2015-04-09 16:45
< >
分享
最佳答案
0

第一次用EF在项目上吧, Dynamic Query LINQ 搜索一下,你会找到一个大神的博客的。记住是英文的。

google 搜索第一条: 

Dynamic LINQ (Part 1: Using the LINQ Dynamic Query Library)

收获园豆:20
爱编程的大叔 | 高人七级 |园豆:30839 | 2015-04-10 09:57
其他回答(5)
0

ef直接拼接不就得了?

吴瑞祥 | 园豆:29449 (高人七级) | 2015-04-09 16:51

因为有些东西需要通过用户的交互来动态的处理 

支持(0) 反对(0) 羊汤大饼 | 园豆:109 (初学一级) | 2015-04-09 17:48

@羊汤大饼: 是啊,动态拼接啊.ef本来就是延迟加载的.

支持(0) 反对(0) 吴瑞祥 | 园豆:29449 (高人七级) | 2015-04-09 23:10
0

LinqKit

收获园豆:5
幻天芒 | 园豆:37175 (高人七级) | 2015-04-09 17:31

谢谢,我回去好好研究下。

支持(0) 反对(0) 羊汤大饼 | 园豆:109 (初学一级) | 2015-04-09 17:49
0

QueryBuilder is your best choice

收获园豆:10
JeffWong | 园豆:2328 (老鸟四级) | 2015-04-09 21:22
0

查询条件总是固定的吧?

var query = (from x in xxx);

if(id) { 

query = query.where(x=>x.id = id)

}

 

我是这样处理

收获园豆:5
Y2zz | 园豆:393 (菜鸟二级) | 2015-04-10 11:03
0
 /// <summary>
        /// 无分页查询
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="orderBy"></param>
        /// <param name="includeProperties"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> Get(
            Expression<Func<TEntity, bool>> filter = null,
            Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
            string includeProperties = "")
        {
            IQueryable<TEntity> query = dbSet;

            if (filter != null)
            {
                query = query.Where(filter);
            }

            foreach (var includeProperty in includeProperties.Split
                (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                query = query.Include(includeProperty);
            }

            if (orderBy != null)
            {
                return orderBy(query).AsNoTracking();
            }
            else
            {
                return query.AsNoTracking();
            }
        }

 public IQueryable<TEntity> GetPaged(out int total, Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>,
          IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "", int index = 0, int size = 50)
        {
            int skipCount = index * size - size;
            var _reset = Get(filter, orderBy, includeProperties);
            total = _reset.Count();
            _reset = skipCount == 0 ? _reset.Take(size) : _reset.Skip(skipCount).Take(size);
            return _reset.AsQueryable();
        }

调用的地方

 

     _RightList = rightRepository.Get(p => 1 == 1 && p.IsShow == true, q => q.OrderBy(j => j.s_Level).ThenBy(j => j.SortNum)).ToList();//获取系统权限表集合
收获园豆:10
小眼睛老鼠 | 园豆:2731 (老鸟四级) | 2015-04-15 17:43
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册