PLinq Lookup ParallelQuery


namespace Microshaoft
{
    using System;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Threading;
    using System.Collections.Generic;
    using System.Collections.Concurrent;
    class Program
    {
        static void Main(string[] args)
        {
            var list = new List<Employee>()
                                    {
                                        new Employee()
                                                {
                                                    ID = 100
                                                    , Name = "Bill Gates"
                                                    , Department = "Microsoft"
                                                }
                                        , new Employee()
                                                {
                                                    ID = 2
                                                    , Name = "Steve Jobs"
                                                    , Department = "Apple"
                                                }
                                        , new Employee()
                                                {
                                                    ID = 300
                                                    , Name = "Larry Page"
                                                    , Department = "Google"
                                                }
                                        , new Employee()
                                                {
                                                    ID = 4
                                                    , Name = "Sergey Brin"
                                                    , Department = "Google"
                                                }
                                        , new Employee()
                                                {
                                                    ID = 300
                                                    , Name = "Larry Page2"
                                                    , Department = "Google"
                                                }
                                        , new Employee()
                                                {
                                                    ID = 4
                                                    , Name = "Microshaoft"
                                                    , Department = "Microsoft"
                                                }
                                    };
            var groups = list.ToLookup<Employee,string>
                                            (
                                                x =>
                                                {
                                                    return x.Department;
                                                }
                                            );
            groups.AsParallel().WithDegreeOfParallelism
                                        (
                                            groups.Count
                                        ).ForAll
                                                (
                                                    (x) =>
                                                    {
                                                        Console.WriteLine("{0},{1}", Thread.CurrentThread.ManagedThreadId, x.Key);
                                                        var orderedGroup = x.OrderByDescending<Employee, int>
                                                                            (
                                                                                (xx) =>
                                                                                {
                                                                                    return xx.ID;
                                                                                }
                                                                            );
                                                        //orderedGroup.AsParallel().WithDegreeOfParallelism(1).ForAll
                                                        //        (
                                                        //            (xxx) =>
                                                        //            {
                                                        //                Console.WriteLine("{0},{1}", Thread.CurrentThread.ManagedThreadId, xxx.Name);
                                                        //            }
                                                        //        );
                                                        orderedGroup.ToList().ForEach
                                                                                (
                                                                                    (xxx) =>
                                                                                    {
                                                                                        Console.WriteLine("{0},{1}", Thread.CurrentThread.ManagedThreadId, xxx.Name);
                                                                                    }
                                                                                );
                                                    }
                                                );
            Console.ReadLine();
        }
        public class Employee
        {
            public int ID
            {
                get;
                set;
            }
            public string Name
            {
                get;
                set;
            }
            public string Department
            {
                get;
                set;
            }
            public string Gender
            {
                get;
                set;
            }
            public DateTime Birthday
            {
                get;
                set;
            }
        }
    }
}

posted @ 2012-06-29 19:59  于斯人也  阅读(283)  评论(0编辑  收藏  举报