编程语言
首页 > 编程语言> > C#中Linq及扩展方法的简单使用

C#中Linq及扩展方法的简单使用

作者:互联网

一、简介

  简单记录一下Linq以及其扩展方法的使用

二、准备工作

  准备好一个Employee类和Department类

 public class Employee
    {
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public int EmployeeAge { get; set; }
        public decimal Salary { get; set; }
        public int DepartmentId { get; set; }
    }

    public class Department
    {
        public int DepartmentId { get; set; }
        public string DepartmentName { get; set; }
    }

  构造数据

List<Employee> Employees = new()
{
    new Employee() { EmployeeName = "机智的小明", DepartmentId = 1, EmployeeAge = 20, EmployeeId = 1, Salary = 15000 },
    new Employee() { EmployeeName = "懒惰的小杨", DepartmentId = 2, EmployeeAge = 20, EmployeeId = 2, Salary = 15000 },
    new Employee() { EmployeeName = "勤劳的小王", DepartmentId = 1, EmployeeAge = 25, EmployeeId = 3, Salary = 15000 },
    new Employee() { EmployeeName = "风雅的小张", DepartmentId = 2, EmployeeAge = 26, EmployeeId = 4, Salary = 15000 },
    new Employee() { EmployeeName = "睿智的小华", DepartmentId = 1, EmployeeAge = 30, EmployeeId = 5, Salary = 15000 },
    new Employee() { EmployeeName = "欧阳峰", DepartmentId = 3, EmployeeAge = 40, EmployeeId = 6, Salary = 15000 },
    new Employee() { EmployeeName = "慕容复", DepartmentId = 3, EmployeeAge = 30, EmployeeId = 7, Salary = 15000 },
};

List<Department> Departments = new()
{
    new Department() { DepartmentId = 1, DepartmentName = "研发部" },
    new Department() { DepartmentId = 2, DepartmentName = "测试部" },
    new Department() { DepartmentId = 3, DepartmentName = "技术支持部" },

};

三、Linq的使用

  3.1 查询所有数据

 var emps = from em in Employees
               select em;
    foreach (var em in emps)
    {
        Console.WriteLine(em.EmployeeName);
    }

  3.2 模糊查询 查询名字中包含小的员工

var emps = from em in Employees
               where em.EmployeeName.Contains('小')
               select em;
    foreach (var em in emps)
    {
        Console.WriteLine(em.EmployeeName);
    }

  3.3 分组查询 按照部门编号分组,筛选条件:编号大于1

var emps = from em in Employees
           where em.DepartmentId > 1
           group em by em.DepartmentId;
foreach (var em in emps)
{
    Console.WriteLine("部门编号:" + em.Key);
    foreach (var item in em)
    {
        Console.WriteLine(item.EmployeeName);
    }
}

  3.4 联合查询 输出员工及其所在的部门

var emps = from em in Employees
           join dpt in Departments
           on em.EmployeeId equals dpt.DepartmentId
           orderby em.EmployeeId descending   //按照员工编号降序
           select new { em.EmployeeId, em.EmployeeName, dpt.DepartmentName } ;
foreach (var em in emps)
{
    Console.WriteLine("编号:" + em.EmployeeId + " 姓名:" + em.EmployeeName + " 部门:" + em.DepartmentName);

}

四、扩展方法

  4.1 所有员工部门编号是否都是1

bool falg = Employees.All(x => x.DepartmentId == 1);

  4.2 是否有员工的部门编号为1

bool  falg = Employees.Any(x => x.DepartmentId == 1);

  4.3 查找部门编号为1的第一个员工

var em = Employees.FirstOrDefault(x => x.DepartmentId == 1);

  4.4 计算部门编号为1的员工人数

long num = Employees.LongCount(x => x.DepartmentId == 1);

  4.5 组合两个集合

var ems = Employees.Zip(Departments, (first, second) =>  
{
    if (first.DepartmentId == second.DepartmentId)
        return first.EmployeeName + second.DepartmentName;
    else
        return first.EmployeeName;
});
foreach (var em in ems)
{
    Console.WriteLine(em);
}

输出结果:

机智的小明研发部 懒惰的小杨测试部 勤劳的小王

  4.6 分组查询

var emps = Employees.GroupBy(x => x.DepartmentId).Where(x => x.Key > 1);
foreach (var em in emps)
{
    Console.WriteLine("部门编号:" + em.Key);
    foreach (var item in em)
    {
        Console.WriteLine(item.EmployeeName);
    }
}

  4.7 联合查询

var emps = Employees.Join(Departments,
        em => em.DepartmentId,
        dpt => dpt.DepartmentId,
        (em, dpt) => new { em.EmployeeName, dpt.DepartmentName });
    foreach (var em in emps)
    {
        Console.WriteLine(em.EmployeeName + em.DepartmentName);
    }

 

标签:em,C#,扩展,Linq,DepartmentId,var,new,EmployeeName,emps
来源: https://www.cnblogs.com/just-like/p/16482501.html