Linq连接查询分组求和、求最大值、最小值、平均值等操作

class Program
    {
        static void Main(string[] args)
        {
            List<Project> plist = new List<Project>() {
                new Project(){ ID = 1,PName = "项目1",MID = 1},
                new Project(){ ID = 2,PName = "项目2",MID = 2},
                new Project(){ ID = 3,PName = "项目3",MID = 3},
                new Project(){ ID = 4,PName = "项目4",MID = 1},
                new Project(){ ID = 5,PName = "项目5",MID = 2},
                new Project(){ ID = 6,PName = "项目6",MID = 3},
           };

            List<Users> ulist = new List<Users>() {
                new Users(){ ID = 1,UName = "员工1"},
                new Users(){ ID = 2,UName = "员工2"},
                new Users(){ ID = 3,UName = "员工3"},
                new Users(){ ID = 4,UName = "员工4"},
                new Users(){ ID = 5,UName = "张三"},
                new Users(){ ID = 6,UName = "员工6"},
                new Users(){ ID = 7,UName = "员工7"},
                new Users(){ ID = 8,UName = "员工8"},
            };

            List<Manager> mlist = new List<Manager>() {
                new Manager(){ ID = 1,MName = "经理1"},
                new Manager(){ ID = 2,MName = "经理2"},
                new Manager(){ ID = 3,MName = "经理3"},
            };

            List<Performance> list = new List<Performance>() {
                new Performance(){ PID = 1,UID = 1,Score = 89},
                new Performance(){ PID = 1,UID = 2,Score = 90},
                new Performance(){ PID = 2,UID = 3,Score = 99},
                new Performance(){ PID = 3,UID = 4,Score = 100},
                new Performance(){ PID = 4,UID = 5,Score = 98},
                new Performance(){ PID = 5,UID = 6,Score = 89},
                new Performance(){ PID = 5,UID = 7,Score = 97},
                new Performance(){ PID = 6,UID = 8,Score = 68},
            };

            //求不同成员的绩效成绩和
            var a = from i in list
                    group i by i.PID
                    into s
                    select new
                    {
                        Key = s.Key,
                        Count = s.Sum(p => p.Score)
                    };

            Console.WriteLine("========================");
            foreach (var item in a)
            {
                Console.WriteLine(item.Key+","+item.Count);
            }

            Console.WriteLine("========================");


            //求不同成员的绩效成绩最大值
            var aa = from i in list
                    group i by i.PID
                    into s
                    select new
                    {
                        Key = s.Key,
                        Count = s.Max(p => p.Score)
                    };

            Console.WriteLine("========================");
            foreach (var item in aa)
            {
                Console.WriteLine(item.Key + "," + item.Count);
            }

            Console.WriteLine("========================");


            //连接查询
            var xx = from jx in list
                     join emp in plist on jx.PID equals emp.ID
                     join user in ulist on jx.UID equals user.ID
                     join jl in mlist on emp.MID equals jl.ID
                     select new
                     {
                         XiangMu = emp.PName,
                         JinglI = jl.MName,
                         UserName = user.UName,
                         JiXiao = jx.Score
                     };

            //分组
            var groups = from x in xx
                         group x by x.XiangMu;


            //输出
            foreach (var item in groups)
            {
                Console.WriteLine(item.Key+"=================================");
                foreach (var i in item)
                {
                    Console.WriteLine(i.XiangMu + "\t" + i.JinglI + "\t" + i.UserName + "\t" + i.JiXiao);
                }
            }

            //最大值
            var max = xx.Max(p => p.JiXiao);
            //最小值
            var min = xx.Min(p => p.JiXiao);
            //平均值
            var avg = xx.Average(p => p.JiXiao);

            //输出
            Console.WriteLine(max+","+min+","+avg);


            Console.ReadLine();
        }

        //项目
        class Project
        {
            public int ID { get; set; }
            public string PName { get; set; }
            public int MID { get; set; }

        }

        //员工
        class Users
        {
            public int ID { get; set; }
            public string UName { get; set; }
        }

        //经理
        class Manager
        {
            public int ID { get; set; }
            public string MName { get; set; }
        }

        //绩效
        class Performance
        {
            public int UID { get; set; }
            public int PID { get; set; }
            public int Score { get; set; }
        }


    }

你可能感兴趣的:(C#,Linq,Linq,C#,连接查询,Group,By)