linq to object使用

Posted wangdash

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linq to object使用相关的知识,希望对你有一定的参考价值。

   //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使用的主要内容,如果未能解决你的问题,请参考以下文章

从LINQ开始之LINQ to Objects(上)

LINQ to OBJECT函数积累

查询表达式和LINQ to Objects

LINQ to SQL class LINQ to sql Objects?

为啥 linq to object 手动实现迭代器?

LINQ to XML - 从文件加载 XML 片段