相关疑难解决方法(0)

IEnumerable <T>/IQueryable <T>上的动态LINQ OrderBy

我在动态LINQ 的VS2008示例中找到了一个示例,它允许您使用类似sql的字符串(例如,OrderBy("Name, Age DESC"))用于排序.不幸的是,包含的方法仅适用于IQueryable<T>;.有没有办法获得此功能IEnumerable<T>

c# linq linq-to-objects

657
推荐指数
12
解决办法
26万
查看次数

在不使用GetMethods的情况下获取泛型方法

我想得到方法System.Linq.Queryable.OrderyBy<T, TKey>(the IQueryable<T> source, Expression<Func<T,TKey>> keySelector)方法,但我不断提出空值.

var type = typeof(T);
var propertyInfo = type.GetProperty(group.PropertyName);
var propertyType = propertyInfo.PropertyType;

var sorterType = typeof(Func<,>).MakeGenericType(type, propertyType);
var expressionType = typeof(Expression<>).MakeGenericType(sorterType);

var queryType = typeof(IQueryable<T>);

var orderBy = typeof(System.Linq.Queryable).GetMethod("OrderBy", new[] { queryType, expressionType }); /// is always null.
Run Code Online (Sandbox Code Playgroud)

有没有人有任何见解?我宁愿不循环GetMethods结果.

.net c# generics reflection

26
推荐指数
5
解决办法
1万
查看次数

标签 统计

c# ×2

.net ×1

generics ×1

linq ×1

linq-to-objects ×1

reflection ×1