C# Linq语句用法大全以及Lambda表达式

C# Linq语句用法大全以及Lambda表达式

Linq: 是一种用于数据查询和操作的语言集成查询(Language Integrated Query)技术。通过 Linq,我们可以使用类似于 SQL 查询的方式来查询、筛选和操作各种类型的数据集合,包括数组、列表、集合、XML 文档、数据库表等等。
常见的有:
Linq to Objects:用于操作对象集合,例如数组、列表等。
Linq to XML:用于操作 XML 数据,支持类似于 XPath 的查询。
Linq to SQL:用于操作关系型数据库,以及进行 SQL 查询。
Linq to Entities:用于操作实体框架数据模型。
Linq to DataSet:用于操作 ADO.NET DataSet 数据集合。

Lambda: Lambda 表达式是一种简洁、方便的语法形式,用于定义匿名函数。它通常用于需要传递一个函数作为参数的场景。

文章主要讲解 Linq 语句的用法

数据准备

新建一个ClassStudent类
属性:ID,班级名称

internal class ClassStudent
{
   public int Id { get; set; }
   public string ClassName { get; set; }
}

初始化ClassStudent

List<ClassStudent> classStudentList = new List<ClassStudent>();
classStudentList.Add(new ClassStudent() { Id = 1, ClassName = "1班" });
classStudentList.Add(new ClassStudent() { Id = 2, ClassName = "2班" });
classStudentList.Add(new ClassStudent() { Id = 3, ClassName = "3班" });

新建一个Student的类
属性:ID,姓名,年龄,ClassStudent.ID

internal class Student
{
   public int Id { get; set; }
   public string Name { get; set; }
   public int Age { get; set; }
}   

初始化Student

List<Student> studentList = new List<Student>();
studentList.Add(new Student() { Id = 1, Name = "妲己", Age = 18, ClassID = 1 });
studentList.Add(new Student() { Id = 2, Name = "申公豹", Age = 28, ClassID = 1 });
studentList.Add(new Student() { Id = 3, Name = "纣王", Age = 38, ClassID = 1 });
studentList.Add(new Student() { Id = 4, Name = "姬发", Age = 20, ClassID = 2 });
studentList.Add(new Student() { Id = 5, Name = "姜子牙", Age = 78, ClassID = 2 });
studentList.Add(new Student() { Id = 6, Name = "哪吒", Age = 56, ClassID = 2 });
studentList.Add(new Student() { Id = 7, Name = "杨戬", Age = 23, ClassID = 3 });
studentList.Add(new Student() { Id = 8, Name = "孙悟空", Age = 1002, ClassID = 3 });

Selcet的用法

查询所有的学生

//Linq语句
var data=from n in studentList select n;
//Lambda表达式
var data = studentList.Select(n => n);
foreach (var student in data) {
    Console.WriteLine($"姓名:{student.Name},年龄:{student.Age}");
}

查询所有的学生的姓名

//Linq语句
var data = from student in students
           select new { student.Name, student.Age };
//Lambda表达式
var data = students.Select(student => new { student.Name, student.Age });
foreach (var student in data) {
    Console.WriteLine($"姓名:{student}");
}

查询年龄大于18岁的学生

//Linq语句
var data = from n in studentList where n.Age > 18 select n;
//Lambda表达式
var data = studentList.Where(n => n.Age > 18);
foreach (var student in data) {
    Console.WriteLine($"姓名:{student.Name},年龄:{student.Age}");
}

根据年龄升序

//Linq语句
var data = from n in studentList orderby n.Age ascending select n;
//Lambda表达式
var data = studentList.OrderBy(n => n.Age);
foreach (var student in data) {
    Console.WriteLine($"姓名:{student.Name},年龄:{student.Age}");
}

根据年龄降序

//Linq语句
var data = from n in studentList orderby n.Age descending select n;
//Lambda表达式
var data = studentList.OrderByDescending(n => n.Age);
foreach (var student in data) {
    Console.WriteLine($"姓名:{student.Name},年龄:{student.Age}");
}

连接查询

//Linq语句
var data = from n in studentList
		   join g in classStudentList on
		   n.ClassID equals g.Id
		   where g.ClassName == "1班"
		   select new { n.Name, g.ClassName };
//Lambda表达式
var data = studentList.Join(
           classStudentList,
           n => n.ClassID,
           g => g.Id,
           (n, g) => new { n.Name, g.ClassName }
           ).Where(x => x.ClassName == "1班");
foreach (var student in data)
{
   Console.WriteLine($"姓名:{student.Name},班级:{student.ClassName}");
}

弱连接查询

//Linq语句
var data = from n in studentList
           join g in classStudentList on n.ClassID equals g.Id
           into x
           from g in x.DefaultIfEmpty()
           select new
           {
              Name = n.Name,
              ClassName = g==null ? "" : g.ClassName
           };
//Lambda表达式
var data = studentList.GroupJoin(
           classStudentList,
           n => n.ClassID,
           g => g.Id,
           (n, group) => new { n, group }
           ).SelectMany(
           x => x.group.DefaultIfEmpty(),
           (x, g) => new
           {
             Name = x.n.Name,
             ClassName = g == null ? "" : g.ClassName
           }
           );
foreach (var student in data)
{
   Console.WriteLine($"姓名:{student.Name},班级:{student.ClassName}");
}

分组用法

//Linq语句
var data = from n in studentList
           group n by n.ClassID into g
           select new
           {
              ClassID = g.Key,
              Age = g.Sum(x => x.Age)
           };
//Lambda表达式
var data = studentList.GroupBy(
           n => n.ClassID,
           (key, group) => new
           {
              ClassID = key,
              Age = group.Sum(x => x.Age)
            }
           );
foreach (var student in data)
{
   Console.WriteLine($"ClassID:{student.ClassID},年龄:
}

分组查询,根据连接classStudentList的ID,查询classStudentList的数量

//linq语句
var data = from n in studentList
            join c in classStudentList on n.ClassID equals c.Id
            into groups
            orderby groups.Count()
            select new
            {
               studentList = n,
               count = groups.Count(),
            };
//Lambda表达式
var data = studentList
            .Join(classStudentList,
                  n => n.ClassID,
                  c => c.Id,
                  (n, c) => new { n, c })
            .GroupBy(x => x.n)
            .Select(g => new 
            { 
                studentList = g.Key, 
                count = g.Count() 
            })
            .OrderBy(x => x.count);
foreach (var student in data)
{
   Console.WriteLine($"姓名:{student.studentList.Name},数量:{student.count }");
}

你可能感兴趣的:(c#,linq,开发语言,.net)