【发布时间】:2012-12-06 06:01:38
【问题描述】:
我对 LINQ 和 PLINQ 还是很陌生。在很多情况下,我通常只使用循环和List.BinarySearch,但我会尽量摆脱这种心态。
public class Staff
{
// ...
public bool Matches(string searchString)
{
// ...
}
}
使用“普通”LINQ - 抱歉,我不熟悉术语 - 我可以执行以下操作:
var matchedStaff = from s
in allStaff
where s.Matches(searchString)
select s;
但我想并行执行:
var matchedStaff = allStaff.AsParallel().Select(s => s.Matches(searchString));
当我检查matchedStaff 的类型时,它是bools 的列表,这不是我想要的。
首先,我在这里做错了什么,其次,我如何从这个查询中返回List<Staff>?
public List<Staff> Search(string searchString)
{
return allStaff.AsParallel().Select(/* something */).AsEnumerable();
}
返回IEnumerable<type>,而不是List<type>。
【问题讨论】:
-
您仍然可以在 PLINQ 中使用查询语法(这就是它的名称):
from s in allStaff.AsParallel() where s.Matches(searchString) select s。