我怎样才能让一个扩展方法,将工作像这样IQueryable的<T>扩展方法不工作
public static class Extensions<T>
{
public static IQueryable<T> Sort(this IQueryable<T> query, string sortField, SortDirection direction)
{
// System.Type dataSourceType = query.GetType();
//System.Type dataItemType = typeof(object);
//if (dataSourceType.HasElementType)
//{
// dataItemType = dataSourceType.GetElementType();
//}
//else if (dataSourceType.IsGenericType)
//{
// dataItemType = dataSourceType.GetGenericArguments()[0];
//}
//var fieldType = dataItemType.GetProperty(sortField);
if (direction == SortDirection.Ascending)
return query.OrderBy(s => s.GetType().GetProperty(sortField));
return query.OrderByDescending(s => s.GetType().GetProperty(sortField));
}
}
目前,上面写着“扩展方法必须在非泛型静态类中定义”。
我该怎么做?
感谢您的答案和额外的链接......我一直试图做动态LINQ整个上午,不知道我真的在做什么。 不能说我现在真的做,要么...需要做一些阅读所有这一切。 再次感谢! – Micah 2010-04-01 20:01:42
我想我们都已经在那里了:o)...祝你好运! – 2010-04-01 20:03:57
顺便说一句,这段代码在这里http://stackoverflow.com/questions/41244/dynamic-linq-orderby确实也有帮助 – Micah 2010-04-01 20:07:41