Entity Framework 第七篇 简化排序
上篇介绍了EF的分页实现,分页的时候会用到排序,但是使用起来表达式写的似乎很繁琐
var a = respository.GetPaged<S_Users>(out count, m => m.LoginName.Contains("a"), q => q.OrderBy(m => m.LastDate).ThenByDescending(m=>m.LoginNum),, );
如果直接使用排序字符串,不更直观简便么?
respository.GetPaged<S_Users>(out count, m => m.LoginName.Contains("a"),"LoginName asc,LoginNum desc", , );
可以通过扩展方法来实现
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 = ; i < sorts.Length; i++)
{
string sortExpression = sorts[i];
int spaceIndex = sortExpression.Trim().IndexOf(" ");
if (spaceIndex < )
{
propertyName = sortExpression;
sortDirection = "ASC";
}
else
{
propertyName = sortExpression.Substring(, spaceIndex);
sortDirection = sortExpression.Substring(spaceIndex + ).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 == )
{
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; }
}
Entity Framework 第七篇 简化排序的更多相关文章
- Entity Framework 学习中级篇1—EF支持复杂类型的实现
本节,将介绍如何手动构造复杂类型(ComplexType)以及复杂类型的简单操作. 通常,复杂类型是指那些由几个简单的类型组合而成的类型.比如:一张Customer表,其中有FristName和Las ...
- Entity Framework学习初级篇2
Entity Framework 学习初级篇2--ObjectContext.ObjectQuery.ObjectStateEntry.ObjectStateManager类的介绍 本节,简单的介绍E ...
- Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 排序、筛选、分页以及分组
Sorting, filtering, paging, and grouping 7 of 8 people found this helpful By Tom Dykstra The Contoso ...
- entity framework 新手入门篇(3)-entity framework实现orderby,count,groupby,like,in,分页等
前面我们已经学习了entityframework的基本的增删改查,今天,我们将在EF中实现一些更加贴近于实际功能的SQL方法. 承接上面的部分,我们有一个叫做House的数据库,其中包含house表和 ...
- entity framework 新手入门篇(2)-entity framework基本的增删改查
经过前两节的简单描述,终于可以进入entity framework的使用部分了.本节将对entity framework原生的增删改查进行讲解. 承接上面的部分,我们有一个叫做House的数据库,其中 ...
- entity framework 新手入门篇(1)-建立模型
entity framework是微软官方免费提供给大家的一套ORM(Object Relational Mapping对象关系映射)解决方案.它不仅可以帮助我们解决数据缓存的问题,还能在最小的开销下 ...
- Entity Framework 第三篇 实体特性声明
Entity Framework中对实体的特性声明有着严格的要求 1.实体必须要有主键特性,但是如果实体没有主键特性那怎么办? public int ExecuteSqlCommand(string ...
- Entity Framework 学习初级篇1--EF基本概况
转自:http://www.cnblogs.com/Tally/archive/2012/09/14/2685011.html 最近在学习研究微软的EF,通过这时间的学习研究,感觉这个EF目前来说还不 ...
- Entity Framework学习初级篇1--EF基本概况《转》
最近在学习研究微软的EF,通过这时间的学习研究,感觉这个EF目前来说还不是很完善,半成品.不过,据说在.Net4.0中,微软将推荐使用此框架,并会有所改善.而且,现在基本上所有数据库均提供了对EF的支 ...
随机推荐
- G面经prepare: Sort String Based On Another
Given a sorting order string, sort the input string based on the given sorting order string. Ex sort ...
- ACdream 1104 瑶瑶想找回文串(SplayTree + Hash + 二分)
Problem Description 刚学完后缀数组求回文串的瑶瑶(tsyao)想到了另一个问题:如果能够对字符串做一些修改,怎么在每次询问时知道以某个字符为中心的最长回文串长度呢?因为瑶瑶整天只知 ...
- linux for java programer
http://www.kancloud.cn/digest/javaxviii/126781 .查找文件 find / -name filename.txt根据名称查找/目录下的filename.tx ...
- 使用Graham扫描法求二维凸包的一个程序
#include <iostream> #include <cstring> #include <cstdlib> #include <cmath> # ...
- [CrunchBang]禁止“桌面上鼠标滚轮切换工作区桌面“
鼠标滚轮切换虚拟桌面相关问题, 编辑 ~/.config/openbox/rc.xml 在 <context name="Desktop">段: <mouse ...
- php curl 抓去远程页面内容
<?php/*** php curl抓取远程网页内容* edit by www.jbxue.com*/$curlPost = 'a=1&b=2';//模拟POST数据$ch = curl ...
- 【海岛帝国系列赛】No.1 海岛帝国:诞辰之日
50111117海岛帝国:诞辰之日 [试题描述] YSF自从上次“被盗投降”完(带着一大堆债)回去以后,YSF对“海盗”怀念至今,他想要建立一个“药师傅”海岛帝国. 今天,他要像“管理部”那样去探寻 ...
- android简单的夜间模式
现在android项目values下打 attrs.xml <?xml version="1.0" encoding="utf-8"?> <r ...
- android 学习随笔二十九(自定义监听 )
package com.itheima.momo.dialog; import com.itheima.momo.R; import android.app.AlertDialog; import a ...
- json校验
直接百度:json在线解析 或 json.cnhttp://json.cn/ json格式校验的.这个更加简洁些.