我已经查询过关于SO的问题,但找不到解决方案。将LINQ查询传递给方法来动态地选择表单集合
在我的程序中是一个类,其唯一原因是从通过构造函数传递给它的数据集合中进行选择。 我需要的是将LINQ查询传递给选择方法以从所述数据中进行选择。但是我没有解决我的问题的运气。 下面是代码:
public class ServiceClient<TInput>
{
private readonly IEnumerable<TInput> data;
public ServiceClient(IEnumerable<TInput> dataInput)
{
this.data = dataInput;
}
public TResult Send<TResult>(Func<IEnumerable<TInput>, TResult> selector)
{
var selectedData = this.data.Select(selector); // How to do the selection here???
// ...
}
}
类被称为是这样的:
private static void Main(string[] args)
{
var persons = new List<Person>
{
new Person { Id = 0, FamilyId = 0 },
new Person { Id = 1, FamilyId = 0 },
new Person { Id = 2, FamilyId = 1 }
};
var func = new Func<IEnumerable<Person>, Person>(Target);
var client = new ServiceClient<Person>(persons);
client.Send(func);
}
private static Person Target(IEnumerable<Person> enumerable)
{
return enumerable.SingleOrDefault(p => p.Id == 0);
}
嗯,这很容易。似乎我必须更熟悉funcs关于该主题的任何好建议? – Ruhrpottpatriot 2015-03-13 15:34:49
我向你推荐一本好书:http://www.amazon.com/Programming-Microsoft%C2%AE-PRO-Developer-Paolo-Pialorsi/dp/0735624003我读过它,发现非常有用。 – 2015-03-14 06:00:45
会试试看,谢谢。 – Ruhrpottpatriot 2015-03-14 19:16:15