并行运行一个简单的LINQ查询 [英] Running a simple LINQ query in parallel

查看:76
本文介绍了并行运行一个简单的LINQ查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对LINQ和PLINQ还是很陌生.通常,在很多情况下,我通常只使用循环和List.BinarySearch,但是我正尽力摆脱这种思维定势.

I'm still very new to LINQ and PLINQ. I generally just use loops and List.BinarySearch in a lot of cases, but I'm trying to get out of that mindset where I can.

public class Staff
{
  // ...
  public bool Matches(string searchString)
  {
    // ...
  }
}

使用普通" LINQ-对不起,我不熟悉该术语-我可以执行以下操作:

Using "normal" LINQ - sorry, I'm unfamiliar with the terminology - I can do the following:

var matchedStaff = from s
                     in allStaff
                  where s.Matches(searchString)
                 select s;

但我想并行执行此操作:

But I'd like to do this in parallel:

var matchedStaff = allStaff.AsParallel().Select(s => s.Matches(searchString));

当我检查matchedStaff的类型时,它是bool的列表,这不是我想要的.

When I check the type of matchedStaff, it's a list of bools, which isn't what I want.

首先,我在这里做错了什么,其次,如何从该查询中返回List<Staff>?

First of all, what am I doing wrong here, and secondly, how do I return a List<Staff> from this query?

public List<Staff> Search(string searchString)
{
  return allStaff.AsParallel().Select(/* something */).AsEnumerable();
}

返回IEnumerable<type>,而不是List<type>.

推荐答案

对于您的第一个问题,您应该将Select替换为Where:

For your first question, you should just replace Select with Where :

var matchedStaff = allStaff.AsParallel().Where(s => s.Matches(searchString));

Select是一个投影运算符,而不是过滤运算符,这就是为什么要得到一个IEnumerable<bool>的原因,它对应于所有Staff对象从输入序列到您的返回布尔值的投影Matches方法调用.

Select is a projection operator, not a filtering one, that's why you are getting an IEnumerable<bool> corresponding to the projection of all your Staff objects from the input sequence to bools returned by your Matches method call.

我了解您完全不使用select可能会很直观,因为您似乎更熟悉查询语法",其中select关键字是强制性的,而使用"lambda语法"则不是这种情况(或流利的语法" ...无论是什么命名),但事实就是这样;)

I understand it can be counter intuitive for you not to use select at all as it seems you are more familiar with the "query syntax" where select keyword is mandatory which is not the case using the "lambda syntax" (or "fluent syntax" ... whatever the naming), but that's how it is ;)

投影运算符,例如Select ,将从序列中的一个元素作为输入,并以某种方式将该元素转换/投影到另一种类型的元素(此处投影为bool类型).而过滤运算符(例如Where )从谓词中获取元素,并根据谓词在输出序列中按原样输出该元素,或者根本不输出该元素

Projections operators, such a Select, are taking as input an element from the sequence and transform/projects this element somehow to another type of element (here projecting to bool type). Whereas filtering operators, such as Where, are taking as input an element from the sequence and either output the element as such in the output sequence or are not outputing the element at all, based on a predicate.

对于您的第二个问题AsEnumerable返回一个IEnumerable,其名称表示;) 如果要获取List<Staff>,则应致电ToList()(如名称所示;)):

As for your second question, AsEnumerable returns an IEnumerable as it's name indicates ;) If you want to get a List<Staff> you should rather call ToList() (as it's name indicates ;)) :

return allStaff.AsParallel().Select(/* something */).ToList();

希望这会有所帮助.

这篇关于并行运行一个简单的LINQ查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆