• 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;
                }
            }
        }
    }
    
    
  • 相关阅读:
    centos安装vim
    thrift学习之二----学习资料积累
    thrift学习之一-------介绍
    组合模式
    一致性哈希算法(consistent hashing)
    php配置php-fpm启动参数及配置详解
    error while loading shared libraries的解決方法
    数据结构之二叉树
    768、最多能完成排序的块(贪心算法)
    VS code 配置C++编译环境
  • 原文地址:https://www.cnblogs.com/Microshaoft/p/2570395.html
Copyright © 2020-2023  润新知