好吧,我已经被这玩意折腾疯了,谁手头上有Expression查询装配器,能否分享一下?
支持条件连接(and,or 带括号分组的,带正序、逆序order的)
第一次用EF在项目上吧, Dynamic Query LINQ 搜索一下,你会找到一个大神的博客的。记住是英文的。
google 搜索第一条:
ef直接拼接不就得了?
因为有些东西需要通过用户的交互来动态的处理
@羊汤大饼: 是啊,动态拼接啊.ef本来就是延迟加载的.
LinqKit
谢谢,我回去好好研究下。
查询条件总是固定的吧?
var query = (from x in xxx);
if(id) {
query = query.where(x=>x.id = id)
}
我是这样处理
/// <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();//获取系统权限表集合