【发布时间】:2011-12-17 01:17:47
【问题描述】:
以下是我做到了多远。我需要将 Expression<Func<T, string>> 作为参数而不是 Func<T,string> 发送到 Get 函数,并且仍然有 Select() 工作。这很容易吗?这是LinqPad 的格式。
void Main()
{
// Setup sample data in wholelist
var wholelist = new List<Example>();
for (var a = 0; a < 10; a++)
{ var t = new Example { id = a.ToString(), name = a.ToString() };
wholelist.Add(t);
}
// Do the real work
var names = Get<Example>( wholelist, p => p.name );
// LinqPad shows content
names.Dump();
}
public class Example
{
public string id {get;set;}
public string name {get;set;}
}
public static List<string>
Get<T>(IEnumerable<T> source, Func<T, string> selector)
{
var list = source.Select<T,string>(selector).ToList();
return list;
}
原因是因为我们已经有很多函数用Expression<Func<T,string>> 调用了这个函数。
【问题讨论】: