respository.GetPaged<S_Users>(out count, m => m.LoginName.Contains("a"),"LoginName asc,LoginNum desc", 1, 20); public IList<TEntity> GetPaged<TEntity>(out int total, Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, int index = 1, int size = 20) where TEntity : class
{
int skipCount = (index - 1) * size;
var query = Get(filter, orderBy);
total = query.Count();
query = skipCount > 0 ? query.Skip(skipCount).Take(size) : query.Take(size);
return query.ToList();
} public IList<TEntity> GetPaged<TEntity>(out int total, Expression<Func<TEntity, bool>> filter = null, string orderBy = null, int index = 1, int size = 20) where TEntity : class
{
int skipCount = (index - 1) * size;
var query = Get(filter, orderBy);
total = query.Count();
query = skipCount > 0 ? query.Skip(skipCount).Take(size) : query.Take(size);
return query.ToList();
} public static class QueryExtensions
{
public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string sortExpressions)
{
if (source == null)
{
throw new ArgumentNullException("source");
} string sortDirection = String.Empty;
string propertyName = String.Empty; sortExpressions = sortExpressions.Trim(); string[] sorts = sortExpressions.Split(',');
for (int i = 0; i < sorts.Length; i++)
{
string sortExpression = sorts[i];
int spaceIndex = sortExpression.Trim().IndexOf(" ");
if (spaceIndex < 0)
{
propertyName = sortExpression;
sortDirection = "ASC";
}
else
{
propertyName = sortExpression.Substring(0, spaceIndex);
sortDirection = sortExpression.Substring(spaceIndex + 1).Trim();
} if (String.IsNullOrEmpty(propertyName))
{
return source;
} ParameterExpression parameter = Expression.Parameter(source.ElementType, String.Empty);
MemberExpression property = Expression.Property(parameter, propertyName);
LambdaExpression lambda = Expression.Lambda(property, parameter); string methodName = string.Empty;
if (i == 0)
{
methodName = (sortDirection.ToUpper() == "ASC") ? "OrderBy" : "OrderByDescending";
}
else
{
methodName = (sortDirection.ToUpper() == "ASC") ? "ThenBy" : "ThenByDescending";
} Expression methodCallExpression = Expression.Call(typeof(Queryable), methodName,
new Type[] { source.ElementType, property.Type },
source.Expression, Expression.Quote(lambda)); source = source.Provider.CreateQuery<T>(methodCallExpression); }
return source; }
}

EF-简化排序的更多相关文章

  1. Entity Framework 第七篇 简化排序

    上篇介绍了EF的分页实现,分页的时候会用到排序,但是使用起来表达式写的似乎很繁琐 , ); 如果直接使用排序字符串,不更直观简便么? respository.GetPaged<S_Users&g ...

  2. EF 随机排序

    /// <summary> /// 数据上下文扩展 /// </summary> public partial class dbDataContext : IUnitOfWor ...

  3. MVC中使用EF:排序,过滤,分页

    原文链接:http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging ...

  4. EF动态排序

    转载的代码,改天再研究 public PageData<T> FindAll(int PageIndex, int PageSize, Expression<Func<T, b ...

  5. EF的简单认识

    EF的简单认识   EF简介 EntityFramwork是微软提供的一款ORM框架(Object Relational Mapping),实体映射模型,它的底层是ADO.NET的机制,使用EF将省去 ...

  6. EF 复杂语句的使用

    //EF多重排序 context.Serials .Where(s => ("," + s.VideoGenreIds + ",").Contains(& ...

  7. Python 字典 列表 嵌套 复杂排序大全

    https://blog.csdn.net/ray_up/article/details/42084863 一: 字典排序 解析: 使用sorted 方法, 排序后的结果为一个元组. 可以字符串排序( ...

  8. 小试牛刀之sort()排序的实现

    受大学室友的鼓动,我也打算利用公众平台来记录自己的前端知识积累,同时呢,自己总结的东西,总归会有局限性,希望小伙伴能给我指点迷津.知识就是一张巨大的网,作为一名摸不清头绪的入学者,唯一能做的事情就是吐 ...

  9. sql查询优化策略

    Sql语句执行顺序: 查询的逻辑执行顺序 (1) FROM left_table (3) join_type JOIN right_table (2) ON join_condition (4) WH ...

  10. .net基本面试题

    OOP: Object Oriented Programming: 面向对象编程技术的关键性观念是它将数据及对数据的操作行为放在一起,作为一个相互依存.不可分割的整体——对象.对于相同类型的对象进行分 ...

随机推荐

  1. MySQL中一致性非锁定读

    一致性非锁定读(consistent nonlocking read)是指InnoDB存储引擎通过多版本控制(multi versionning)的方式来读取当前执行时间数据库中行的数据,如果读取的行 ...

  2. python函数回顾:dict()

    描述 dict() 函数用于创建一个字典. 语法 dict 语法: class dict(**kwarg) class dict(mapping, **kwarg) class dict(iterab ...

  3. [Erlang危机](5.1.3)进程

    原创文章,转载请注明出处:server非业余研究http://blog.csdn.net/erlib 作者Sunface 联系邮箱:cto@188.com Processes Trying to ge ...

  4. Spring Boot在aop中获取request对象

    doBefore(){ ServetRequestAttrbtes attributes = (ServetRequestAttrbtes)RequestContextHolder.getHttpat ...

  5. Android之四大组件、六大布局、五大存储

    [-] Android六大界面布局方式 1 LinearLayout线性布局 LinearLayout的常用XML属性及相关方法 LinearLayout子元素支持的常用XML属性及方法 2 Tabl ...

  6. LeNet5

    Lecun Y, Bottou L, Bengio Y, et al. Gradient-based learning applied to document recognition[J]. Proc ...

  7. 【jenkins】jenkins实时显示python脚本输出

    jenkins在构建shell脚本时可以实时输出结果,但是在构建python脚本时,是等到python执行完成以后,才显示结果,这个对于我们判断脚本执行状态非常不利 这里介绍一种方法,能够实时显示py ...

  8. php基础知识测试总结

    1.LAMP具体结构包括Linux系统,Apache服务器,MySQL数据库,PHP语言. WAMP具体结构包括Windows系统,Apache服务器,MySQL数据库,PHP语言. 2.B/S架构: ...

  9. 【CodeChef】Enormous Input Test

    The purpose of this problem is to verify whether the method you are using to read input data is suff ...

  10. CSS3手风琴菜单 可同时折叠多个菜单

    在线演示 本地下载