码迷,mamicode.com
首页 > 其他好文 > 详细

linq to object使用

时间:2019-11-12 00:48:12      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:summary   group by   科学家   default   erb   数据   nta   where   var   

   //linq to object
    public class LinqShow
    {
        #region Data Init
        private List<Student> GetStudentList()
        {
            #region 初始化数据
            List<Student> studentList = new List<Student>()
            {
                new Student()
                {
                    Id=1,
                    Name="张三",
                    ClassId=6,
                    Age=35
                },
                new Student()
                {
                    Id=1,
                    Name="李四",
                    ClassId=2,
                    Age=23
                },
                 new Student()
                {
                    Id=1,
                    Name="王炸",
                    ClassId=2,
                    Age=27
                },
                 new Student()
                {
                    Id=1,
                    Name="疯子科学家",
                    ClassId=2,
                    Age=26
                },
                new Student()
                {
                    Id=1,
                    Name="",
                    ClassId=2,
                    Age=25
                },
                new Student()
                {
                    Id=1,
                    Name="王五",
                    ClassId=2,
                    Age=24
                },
                new Student()
                {
                    Id=1,
                    Name="故乡的风",
                    ClassId=2,
                    Age=21
                },
                 new Student()
                {
                    Id=1,
                    Name="晴天",
                    ClassId=2,
                    Age=22
                },
                 new Student()
                {
                    Id=1,
                    Name="旭光",
                    ClassId=2,
                    Age=34
                },
                 new Student()
                {
                    Id=1,
                    Name="oldkwok",
                    ClassId=2,
                    Age=30
                },
                new Student()
                {
                    Id=1,
                    Name="乐儿",
                    ClassId=2,
                    Age=30
                },
                new Student()
                {
                    Id=1,
                    Name="暴风轻语",
                    ClassId=2,
                    Age=30
                },
                new Student()
                {
                    Id=1,
                    Name="一个人的孤单",
                    ClassId=2,
                    Age=28
                },
                new Student()
                {
                    Id=1,
                    Name="小张",
                    ClassId=2,
                    Age=30
                },
                 new Student()
                {
                    Id=3,
                    Name="阿亮",
                    ClassId=3,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="37度",
                    ClassId=4,
                    Age=30
                }
                  ,
                  new Student()
                {
                    Id=4,
                    Name="关耳",
                    ClassId=4,
                    Age=30
                }
                  ,
                  new Student()
                {
                    Id=4,
                    Name="耳机侠",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="Wheat",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="Heaven",
                    ClassId=4,
                    Age=22
                },
                  new Student()
                {
                    Id=4,
                    Name="等待你的微笑",
                    ClassId=4,
                    Age=23
                },
                  new Student()
                {
                    Id=4,
                    Name="",
                    ClassId=4,
                    Age=25
                },
                  new Student()
                {
                    Id=4,
                    Name="混无痕",
                    ClassId=4,
                    Age=26
                },
                  new Student()
                {
                    Id=4,
                    Name="37度",
                    ClassId=4,
                    Age=28
                },
                  new Student()
                {
                    Id=4,
                    Name="新的世界",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="Rui",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="肩膀",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="根号三",
                    ClassId=4,
                    Age=30
                }
            };
            #endregion
            return studentList;
        }
        #endregion

        public void Show()
        {
            List<Student> studentList = this.GetStudentList();

            #region 投影成一个新的对象
            {
                var list = studentList.Where<Student>(s => s.Age < 30).Select(s => new
                {
                    IdName = s.Id + s.Name,
                    ClassName = s.ClassId == 1 ? "一班" : "二班"
                });
            }
            {
                var list = from s in studentList
                           where s.Age < 30
                           select new
                           {
                               IdName = s.Id + s.Name,
                               ClassName = s.ClassId == 1 ? "一班" : "二班"
                           };
            }
            #endregion
            #region In查询
            {
                var list = studentList.Where<Student>(s => new int[] { 1, 2, 3 }.Contains(s.ClassId)).Select(s => new
                {
                    IdName = s.Id + s.Name,
                    ClassName = s.ClassId == 1 ? "一班" : "二班"
                });
            }
            #endregion
            #region 排序分页
            {
                var list = studentList.Where<Student>(s => s.Age < 30)//条件过滤
                                 .Select(s => new//投影
                                 {
                                     s.Id,
                                     s.ClassId,
                                     IdName = s.Id + s.Name,
                                     ClassName = s.ClassId == 1 ? "一班" : "二班"
                                 })
                                 .OrderBy(s => s.Id)//排序
                                                    //.ThenBy//2个都生效
                                 .OrderByDescending(s => s.ClassId)//倒排  最后一个生效
                                 .Skip(2)//跳过几条
                                 .Take(3);//获取几条
            }
            #endregion
            #region group by
            {
                var list = from s in studentList
                           where s.Age < 30
                           group s by s.ClassId into sg
                           select sg;
                foreach (var data in list)
                {
                    Console.WriteLine(data.Key);
                    foreach (var item in data)
                    {
                        Console.WriteLine($"{item.Id} {item.Name} {item.Age}");
                    }
                }
            }
            {
                var list = from s in studentList
                           where s.Age < 30
                           group s by s.ClassId into sg
                           select new
                           {
                               key = sg.Key,
                               maxAge = sg.Max(t => t.Age)
                           };
                foreach (var item in list)
                {
                    Console.WriteLine($"key={item.key}  maxAge={item.maxAge}");
                }
                //group by new {s.ClassId,s.Age}
                //group by new {A=s.ClassId>1}
            }
            {
                var list = studentList.GroupBy(s => s.ClassId).Select(sg => new
                {
                    key = sg.Key,
                    maxAge = sg.Max(t => t.Age)
                });
                //new { s.ClassId, s.Age }
            }
            #endregion

            List<Class> classList = new List<Class>()
                {
                    new Class()
                    {
                        Id=1,
                        ClassName="一班"
                    },
                    new Class()
                    {
                        Id=2,
                        ClassName="二班"
                    },
                    new Class()
                    {
                        Id=3,
                        ClassName="三班"
                    },
                };

            #region 左连接
            {
                var list = from s in studentList
                           join c in classList on s.ClassId equals c.Id//不能用==只能equals
                           select new
                           {
                               Name = s.Name,
                               CalssName = c.ClassName
                           };
                foreach (var item in list)
                {
                    Console.WriteLine($"Name={item.Name},CalssName={item.CalssName}");
                }
            }
            {
                var list = studentList.Join(classList, s => s.ClassId, c => c.Id, (s, c) => new
                {
                    Name = s.Name,
                    CalssName = c.ClassName
                });
                foreach (var item in list)
                {
                    Console.WriteLine($"Name={item.Name},CalssName={item.CalssName}");
                }
            }
            {
                var list = from s in studentList
                           join c in classList on s.ClassId equals c.Id
                           into scList
                           from sc in scList.DefaultIfEmpty()//
                           select new
                           {
                               Name = s.Name,
                               CalssName = sc == null ? "无班级" : sc.ClassName//c变sc,为空则用
                           };
                foreach (var item in list)
                {
                    Console.WriteLine($"Name={item.Name},CalssName={item.CalssName}");
                }
                Console.WriteLine(list.Count());
            }
            {
                var list = studentList.Join(classList, s => s.ClassId, c => c.Id, (s, c) => new
                {
                    Name = s.Name,
                    CalssName = c.ClassName
                }).DefaultIfEmpty();//为空就没有了
                foreach (var item in list)
                {
                    Console.WriteLine($"Name={item.Name},CalssName={item.CalssName}");
                }
            }
            #endregion
        }
    }


    /// <summary>
    /// 学生实体
    /// </summary>
    public class Student
    {
        public int Id { get; set; }
        public int ClassId { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
    /// <summary>
    /// 班级实体
    /// </summary>
    public class Class
    {
        public int Id { get; set; }
        public string ClassName { get; set; }
    }

 

linq to object使用

标签:summary   group by   科学家   default   erb   数据   nta   where   var   

原文地址:https://www.cnblogs.com/wangdash/p/11839235.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!