【发布时间】:2012-10-17 19:56:15
【问题描述】:
我在网页中显示一个对象表Company,我正在使用动态 Linq OrderBy 对每个属性进行排序。 我正在使用此代码https://stackoverflow.com/a/233505/265122
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderBy");
}
public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderByDescending");
}
public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenBy");
}
public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenByDescending");
}
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;
}
这很好,但我还想根据员工人数对公司进行排序。
像这样:query.OrderBy("Employees.Count")
到目前为止,我尝试动态调用 Count 方法,但没有成功。
我修改了这样的代码:
foreach(string prop in props)
{
if (prop == "Count")
{
var countMethod = (typeof(Enumerable)).GetMethods().First(m => m.Name == "Count").MakeGenericMethod(type);
expr = Expression.Call(countMethod, expr);
break;
}
// Use reflection (not ComponentModel) to mirror LINQ.
PropertyInfo pi = type.GetProperty(prop);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
}
但我在expr = Expression.Call(countMethod, expr); 上有一个例外
例外是:
ArgumentException
Expression of type 'System.Collections.Generic.ICollection`1[Employee]'
cannot be used for parameter of type
'System.Collections.Generic.IEnumerable`1[System.Collections.Generic.ICollection`1
[Employee]]' of method 'Int32 Count[ICollection`1]
System.Collections.Generic.IEnumerable`1[System.Collections.Generic.ICollection`1
Employee]])'
你知道如何实现吗?
【问题讨论】:
-
你遇到了什么异常?
-
expr和type的值是多少? -
其余代码在此处stackoverflow.com/questions/41244/dynamic-linq-orderby/… expr 是 Expression 类型,在我的示例中类型为“Employee”。为什么投反对票?
-
好吧,我刚刚在 Linq To Entities 和 Linq To Object 中进行了测试,Marc Gravell 的代码在这两种情况下都可以正常运行,无需更改。您确定您的公司课程中有
public virtual IList<Employee> Employees吗? -
你确定是拉斐尔吗?我正在使用 Linq to Entities,但我也使用 Linq To Object 和像“Employees.Count”这样的字符串进行了测试,它不起作用。我用这段代码测试过:gist.github.com/3899759