linq sort
来源:互联网 发布:淘宝店怎么才能有生意 编辑:程序博客网 时间:2024/06/08 04:37
传入排序的格式为 it.groupid desc 或 it.groupname asc 等
static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property)
{
string methodName = "OrderBy";
string[] props = property.Split('.'); Type type = typeof(T);
ParameterExpression arg = Expression.Parameter(type, "x"); Expression expr = arg;
string prop = props[1].Split(' ')[0];
string sort = props[1].Split(' ')[1];
if (sort.Equals("desc"))
{
methodName = "OrderByDescending";
}
if (sort.Equals("asc"))
{
methodName = "OrderBy";
}
PropertyInfo pi = type.GetProperty(prop);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
//foreach (string prop in props)
//{ // use reflection (not ComponentModel) to mirror LINQ
// PropertyInfo pi = type.GetProperty(prop);
// expr = Expression.Property(expr, pi);
// type = pi.PropertyType;
//}
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);
object result = typeof(Queryable).GetMethods().Single(
method => method.Name == methodName
&& method.IsGenericMethodDefinition
&& method.GetGenericArguments().Length == 2
&& method.GetParameters().Length == 2)
.MakeGenericMethod(typeof(T), type)
.Invoke(null, new object[] { source, lambda });
return (IOrderedQueryable<T>)result;
}
调用:
public IQueryable<gps_drivers> GetGps_driversBySort(string sort, string keyword, string userloginname)
{
var item=from x in ObjectContext.gps_drivers
join y in this.ObjectContext.gps_group_users on x.GroupID equals y.GroupID
where y.UserLoginName.Equals(userloginname)
select x ;
int i = item.Count();
return ApplyOrder<gps_drivers>(item, sort);
}
参考 http://stackoverflow.com/questions/41244/dynamic-linq-orderby
static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName) {
string[] props = property.Split('.');
Type type = typeof(T);
ParameterExpression arg = Expression.Parameter(type, "x");
Expression expr = arg; foreach(string prop in props)
{ // use reflection (not ComponentModel) to mirror LINQ
PropertyInfo pi = type.GetProperty(prop);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
}
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);
object result = typeof(Queryable).GetMethods().Single(
method => method.Name == methodName
&& method.IsGenericMethodDefinition
&& method.GetGenericArguments().Length == 2
&& method.GetParameters().Length == 2)
.MakeGenericMethod(typeof(T), type)
.Invoke(null, new object[] { source, lambda });
return (IOrderedQueryable<T>)result;
}
- linq sort
- bubble sort and linq sort
- 简单比较linq sort的效率,其实性能很低
- LINQ
- LINQ
- LINQ
- LINQ
- LINQ
- LINQ
- LINQ
- LINQ
- LINQ
- LINQ
- LINQ
- LINQ
- LINQ
- LINQ
- LINQ
- Career Cup 1-7
- 不通过App Store,在iOS设备上直接安装应用程序
- 友元函数
- Ssh整合开发介绍和简单的登入案例实现
- Ado.net批量添加和更新数据简单示例
- linq sort
- C#事件和Unity3D
- tfs配置方法
- Maven的使用说明
- 过桥问题和倒水问题
- js清除input中type等于file的值域
- 分析RF电路设计中的常见问题
- wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString....
- Android 手机上获取物理唯一标识码