我正在List<Boss>收藏,每个老板都有2到10个助理人员。我正在将所有员工(包括老板)分组。现在,我正在使用Parallel LINQList<Person>搜索“ Raj”,在Where子句之前或之后,可以在哪里放置支持方法以获得更好的性能?AsParallel()
public class Person
{
    public int EmpID { get; set; }
    public string Name { get; set; }
    public string Department { get; set; }
    public string Gender { get; set; }
}
void Main()
{
    List<Boss> BossList = new List<Boss>()
    {
        new Boss()
        {
            EmpID = 101,
            Name = "Harry",
            Department = "Development",
            Gender = "Male",
            Employees = new List<Person>()
            {
                new Person() {EmpID = 102, Name = "Peter", Department = "Development",Gender = "Male"},
                new Person() {EmpID = 103, Name = "Emma Watson", Department = "Development",Gender = "Female"},
            }
        },
        new Boss()
        {
            EmpID = 104,
            Name = "Raj",
            Department = "Development",
            Gender = "Male",
            Employees = new List<Person>()
                    {
                        new Person() {EmpID = 105, Name = "Kaliya", Department = "Development",Gender = "Male"},
                        new Person() {EmpID = 103, Name = "Emma Watson", Department = "Development",Gender = "Female"},
                    }
        }
    };
    List<Person> result = BossList
    .SelectMany(x =>
        new[] { new Person { Name = x.Name, Department = x.Department, Gender = x.Gender, EmpID = x.EmpID } }
        .Concat(x.Employees))
    .GroupBy(x => x.EmpID) //Group by employee ID
    .Select(g => g.First()) //And select a single instance for each unique employee
    .ToList();
    List<Person> SelectedResult = new List<Person>();
    // AsParallel() - Before Where Clause
    SelectedResult = result.AsParallel().Where(m => m.Name.ToLowerInvariant().Contains("Raj".ToLowerInvariant())).ToList();
    // AsParallel() - After Where Clause
    SelectedResult = result.Where(m => m.Name.ToLowerInvariant().Contains("Raj".ToLowerInvariant())).AsParallel().ToList();
}
核心源代码:
    List<Person> SelectedResult = new List<Person>();
    // AsParallel() - Before Where Clause
    SelectedResult = result.AsParallel().Where(m => m.Name.ToLowerInvariant().Contains("Raj".ToLowerInvariant())).ToList();
    // AsParallel() - After Where Clause
    SelectedResult = result.Where(m => m.Name.ToLowerInvariant().Contains("Raj".ToLowerInvariant())).AsParallel().ToList();
之前。
AsParallel帮助我们并行运行查询,这使并行线程能够提高性能。如果将WHERE子句放在前面,则过滤将顺序进行,然后才能并行化任何内容。
这是一些测试代码:
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
class AsParallelTest
{
    static void Main()
    {
        var query = Enumerable.Range(0, 1000)
                              .Select(ProjectionExample)
                              .Where(x => x > 10)
                              .AsParallel();
        Stopwatch stopWatch = Stopwatch.StartNew();
        int count = query.Count();
        stopWatch.Stop();
        Console.WriteLine("Count: {0} in {1}ms", count,
                          stopWatch.ElapsedMilliseconds);
        query = Enumerable.Range(0, 1000)
                          .AsParallel()
                          .Select(ProjectionExample)
                          .Where(x => x > 10);
        stopWatch = Stopwatch.StartNew();
        count = query.Count();
        stopWatch.Stop();
        Console.WriteLine("Count: {0} in {1}ms", count,
                       stopWatch.ElapsedMilliseconds);
   }
   static int ProjectionExample(int arg)
   {
       Thread.Sleep(10);
       return arg;
   }
}
结果:
Count: 989 in 10574ms
Count: 989 in 1409ms
显然,第一个结果尚未并行化,而第二个已并行化。如果只有一个处理器核心,则结果应该接近。如果您具有两个以上的处理器核心,则AsParallel调用可能会进一步提高性能。此外,您还可以阅读此文章。