【发布时间】:2014-02-06 10:24:27
【问题描述】:
我正在尝试使用泛型创建一个扩展方法,您会在其中收到一个列表 任何类,例如:
class User
{
public string name {get;set;}
public int age {get;set;}
public string lastLogon {get;set;}
}
我正在尝试构建一个表达式,您可以在其中将 IEnumerable 传递给扩展方法,并指定哪个是字符串日期属性。 (在这种情况下lastLogon)
没有扩展方法应该是这样的:
userList.OrderBy(x => Convert.ToDateTime(x.lastLogon));
这是我到目前为止所做的:
public static IEnumerable<TSource> OrderEnumerablebyDatetimeString<TSource,TKey>(
this IEnumerable<TSource> input, Func<TSource, TKey> funcexpr)
{
//modify expression to get the date field and modify the lambda expr
Expression<Func<TSource, TKey>> expr = ???
return input.OrderBy(funcexpr);
}
我的最终分机电话应如下所示:
userList.OrderEnumerablebyDatetimeString(x => x.lastLogon);
【问题讨论】:
-
使用
Expression<Func<TSource, TKey>> funcexpr而不是Func<TSource, TKey> funcexpr -
Grundy 是对的,如果这是 Linq-to-SQL。
-
@JeppeStigNielsen 我的意思是修改后调用
Compile并使用IEnumerable -
这是对象的 linq
标签: c# .net lambda linq-to-objects func