c# Lambda表达结合Linq的学习整理

学习整理!

定义基础类

    /// <summary>
    /// 球员类
    /// </summary>
    public class Player
    {
        public int ID { get; set; }

        public string Name { get; set; }

        public int Age { get; set; }

        /// <summary>
        /// 所属位置
        /// </summary>
        public Location Location { get; set; }

        /// <summary>
        /// 所属球队
        /// </summary>
        public Team Team { get; set; }

        public int MvpNumber { get; set; }

    }
    /// <summary>
    /// 位置类
    /// </summary>
    public class Location
    {

        public string LocationCode { get; set; }

        public string LocationName { get; set; }
    }
}
    /// <summary>
    /// 球队类
    /// </summary>
    public class Team
    {
        public string TeamCode { get; set; }

        public string TeamName { get; set;  }
    }

添加数据

            //初始化测试数据
            List<Player> players = new List<Player>()
            {
                new Player { ID = 1, Name = "Harden", Age = 30,
                    Location = new Location { LocationCode = "PG", LocationName = "控球" },
                    Team = new Team { TeamCode = "HOU", TeamName = "休斯顿火箭" }, MvpNumber = 1
                },
                   new Player { ID = 2, Name = "Curry", Age = 29,
                    Location = new Location { LocationCode = "SG", LocationName = "得分" },
                    Team = new Team { TeamCode = "GSW", TeamName = "金州勇士" }, MvpNumber = 2
                },
                      new Player { ID = 3, Name = "James", Age = 33,
                    Location = new Location { LocationCode = "SF", LocationName = "小前" },
                    Team = new Team { TeamCode = "LAL", TeamName = "洛杉矶湖人" }, MvpNumber = 5
                },
                         new Player { ID = 4, Name = "Zhouqi", Age = 20,
                    Location = new Location { LocationCode = "PF", LocationName = "大前" },
                    Team = new Team { TeamCode = "CHN", TeamName = "铁头娃队" }, MvpNumber = 0
                },
                            new Player { ID = 5, Name = "Yao", Age = 36,
                    Location = new Location { LocationCode = "C", LocationName = "中锋" },
                    Team = new Team { TeamCode = "CHN", TeamName = "无敌天朝队" }, MvpNumber = 8
                }
            };

开始测试

引入 using System.Linq;

一、Max/Min(最大值/最小值) 

        var ageMax = players.Max(u => u.Age);
            Console.WriteLine("最大年龄:" + ageMax);

            var ageMin = players.Min(u => u.Age);
            Console.WriteLine("最小年龄:" + ageMin);

 结果:c# Lambda表达结合Linq的学习整理

二、Where条件筛选

Method 1 :源数据.Where( u => u.属性 筛选条件);

Method 2:from 变量 in 源数据 

     Where 变量.属性 筛选条件

     Select 变量;

ps: 可加ToList()方法带出数据(个人比较Low的解释)

            //查询MvpNumber值大于2的球员
            //第一种写法
            var mvpNum1 = players.Where(u => u.MvpNumber > 2).ToList();  

            //第二种写法
            var mvpNum2 = (from n in players
                           where n.MvpNumber > 2
                           select n).ToList();

输出:c# Lambda表达结合Linq的学习整理

三、排序 OrderBy

 升序:源数据.OrderBy( u => u.属性).ToList();

 降序:源数据.OrderByDescending( u => u.属性).ToList();

ps:必须包含ToList()方法,否则报类型错误

          //按年龄升序排序
            players = players.OrderBy(u => u.Age).ToList();
            //按年龄降序排序
            players = players.OrderByDescending(u => u.MvpNumber).ToList();

结果:c# Lambda表达结合Linq的学习整理

四、分组GroupBy

 Method 1:源数据.GroupBy( u => u.属性).ToList();

 Method 2:from n in 源数据

      group n by n.属性

      into newGroup

      Select newGroup;

            //按球队分组
            Console.WriteLine("\n方法一:");

            var teamGroup = players.GroupBy(u => u.Team.TeamName).ToList();

            Console.WriteLine("共有{0}支球队", teamGroup.Count());
            foreach (var item in teamGroup)
            {
                Console.WriteLine("球队名称:{0}", item.Key);

                foreach (var n in item)
                {
                    Console.WriteLine(n.ID + "\t" + n.Name + "\t" + n.Age + "\t" +
                        n.Location.LocationCode + "\t" + n.Location.LocationName + "\t" +
                        n.Team.TeamCode + "\t" + n.Team.TeamName + "\t" + n.MvpNumber);
                }
            }

       
            Console.WriteLine("\n方法二:");

            var teamGroup2 = (from n in players
                              group n by n.Team.TeamName
                           into newGroup
                              select newGroup).ToList();

            Console.WriteLine("共有{0}支球队", teamGroup2.Count());
            foreach (var item in teamGroup2)
            {
                Console.WriteLine("球队名称:{0}", item.Key);

                foreach (var n in item)
                {
                    Console.WriteLine(n.ID + "\t" + n.Name + "\t" + n.Age + "\t" +
                        n.Location.LocationCode + "\t" + n.Location.LocationName + "\t" +
                        n.Team.TeamCode + "\t" + n.Team.TeamName + "\t" + n.MvpNumber);
                }
            }

结果:c# Lambda表达结合Linq的学习整理

 

Linq Group By 用法总结

Key、Count

 

           var q = (from n in players
                     group n by n.Team.TeamName
                     into g
                     select g).ToList();


            Console.WriteLine("\n共{0}支球队", q.Count()); //如果上面进行ToList操作,q.Count可以不加();
            foreach (var item in q)
            {
                Console.WriteLine("球队名称:" + item.Key);
            }

 

结果:c# Lambda表达结合Linq的学习整理 c# Lambda表达结合Linq的学习整理

 

Group By中使用Where约束

            //30岁以上年龄段分组
            var q = (from n in players
                    group n by n.Age
                    into g
                    where g.Key > 30
                    select g).ToList();

结果:c# Lambda表达结合Linq的学习整理

 

Group By根据表达式分组

            //年龄大于30岁且MVP数量大于1分为一组,否则分在另一组
            var q = (from n in players
                     group n by n.Age > 30 && n.MvpNumber >= 1
                    into g
                     select g).ToList();
            foreach (var item in q)
            {

                if (item.Key)
                {
                    Console.WriteLine("满足条件:");
                    foreach (var n in item)
                    {
                        Console.WriteLine(n.ID + "\t" + n.Name + "\t" + n.Age + "\t" + 
                            n.Location.LocationCode + "\t" + n.Location.LocationName + "\t" +
                            n.Team.TeamCode + "\t" + n.Team.TeamName + "\t" + n.MvpNumber);
                    }
                }
                else
                {
                    Console.WriteLine("不满足条件:");
                    foreach (var n in item)
                    {
                        Console.WriteLine(n.ID + "\t" + n.Name + "\t" + n.Age + "\t" +
                        n.Location.LocationCode + "\t" + n.Location.LocationName + "\t" +
                        n.Team.TeamCode + "\t" + n.Team.TeamName + "\t" + n.MvpNumber);
                    }
                }
            }

结果:c# Lambda表达结合Linq的学习整理

输出:c# Lambda表达结合Linq的学习整理

 

 Group By 多列

 

            var q = (from n in players
                     group n by new
                     {
                         //这里的Key包含Age,Name以及Team信息
                         n.Age,
                         n.Name,
                         n.Team
                     }
                    into g
                     select g).ToList();

 

结果:c# Lambda表达结合Linq的学习整理

 

  ego cogito ergo sum!

 

上一篇:Eclipse 设置 SVN TEAM 同步 忽略文件或文件夹


下一篇:5分钟快速部署PESCMS TEAM 团队任务管理系统开发版