如何:创建并执行简单的 PLINQ 查询

更新:2010 年 5 月

下面的示例演示如何通过对源序列使用 AsParallel 扩展方法来创建一个简单的并行 LINQ 查询,并使用 ForAll<TSource> 方法执行该查询。

注意注意

本文档使用 lambda 表达式在 PLINQ 中定义委托。如果您不熟悉 C# 或 Visual Basic 中的 lambda 表达式,请参见 在 PLINQ 和 TPL 中的 Lambda 表达式

示例

Sub SimpleQuery()

    Dim source = Enumerable.Range(100, 20000)

    ' Result sequence might be out of order.
    Dim parallelQuery = From num In source.AsParallel()
                            Where num Mod 10 = 0
                            Select num

    ' Process result sequence in parallel
    parallelQuery.ForAll(Sub(e)
                             DoSomething(e)
                         End Sub)

    ' Or use For Each to merge results first
    ' as in this example, Where results must
    ' be serialized sequentially through static Console method.
    For Each n In parallelQuery
        Console.Write("{0} ", n)
    Next

    ' You can also use ToArray, ToList, etc
    ' as with LINQ to Objects.
    Dim parallelQuery2 = (From num In source.AsParallel()
                              Where num Mod 10 = 0
                              Select num).ToArray()

    'Method syntax is also supported
    Dim parallelQuery3 = source.AsParallel().Where(Function(n)
                                                       Return (n Mod 10) = 0
                                                   End Function).Select(Function(n)
                                                                            Return n

                                                                        End Function)

    For Each i As Integer In parallelQuery3
        Console.Write("{0} ", i)
    Next
    Console.ReadLine()

End Sub

' A toy function to demonstrate syntax. Typically you need a more
' computationally expensive method to see speedup over sequential queries.
Sub DoSomething(ByVal i As Integer)
    Console.Write("{0:###.## }", Math.Sqrt(i))
End Sub
            var source = Enumerable.Range(100, 20000);

            // Result sequence might be out of order.
            var parallelQuery = from num in source.AsParallel()
                                where num % 10 == 0
                                select num;

            // Process result sequence in parallel
            parallelQuery.ForAll((e) => DoSomething(e));

            // Or use foreach to merge results first.
            foreach (var n in parallelQuery)
            {
                Console.WriteLine(n);
            }

            // You can also use ToArray, ToList, etc
            // as with LINQ to Objects.
            var parallelQuery2 = (from num in source.AsParallel()
                                  where num % 10 == 0
                                  select num).ToArray();

            // Method syntax is also supported
            var parallelQuery3 = source.AsParallel().Where(n => n % 10 == 0).Select(n => n);

此示例演示用于在结果序列的排序不重要的情况下创建和执行任何并行 LINQ 查询的基本模式;未排序的查询通常比已排序的查询快。 查询将源分区为多个任务,这些任务将在多个线程上异步执行。 每个任务的完成顺序不仅取决于处理分区中的元素所涉及的工作量,还取决于诸如操作系统如何调度每个线程之类的外部因素。 本示例旨在演示用法,运行速度可能不如等效的顺序 LINQ to Objects 查询快。 有关加速的更多信息,请参见了解 PLINQ 中的加速。 有关如何在查询中保留元素排序的更多信息,请参见如何:在 PLINQ 查询中控制排序

编译代码

  • 创建控制台应用程序项目

  • 将代码示例粘贴在 Main 后面。

  • 在 Main 添加一个对 SimpleQuery 的调用,然后按 F5。

请参见

概念

并行 LINQ (PLINQ)

修订记录

日期

修订记录

原因

2010 年 5 月

添加了有关用法与 加速的注释。

客户反馈