c# LINQ表达式

发布于:2025-04-16 ⋅ 阅读:(13) ⋅ 点赞:(0)

LINQ(Language Integrated Query)即语言集成查询,是 C# 的一项强大特性,它允许你使用类似 SQL 的语法对各种数据源(如集合、数据库等)进行查询操作。下面为你详细介绍 LINQ 的一些常见用法:

1. 查询语法

查询语法类似于 SQL,使用fromwhereselect等关键字来构建查询。

csharp

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        // 示例数据
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        // 查询所有偶数
        var evenNumbers = from num in numbers
                          where num % 2 == 0
                          select num;

        // 输出结果
        foreach (var num in evenNumbers)
        {
            Console.WriteLine(num);
        }
    }
}

在这个例子中,from num in numbers指定了数据源,where num % 2 == 0筛选出偶数,select num选择要返回的元素。

2. 方法语法

方法语法使用扩展方法来构建查询,例如WhereSelect等。

csharp

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        // 示例数据
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        // 查询所有偶数
        var evenNumbers = numbers.Where(num => num % 2 == 0);

        // 输出结果
        foreach (var num in evenNumbers)
        {
            Console.WriteLine(num);
        }
    }
}

这里使用Where方法筛选出偶数,num => num % 2 == 0是一个 Lambda 表达式,用于定义筛选条件。

3. 投影操作

投影操作可以将查询结果转换为新的类型。

csharp

using System;
using System.Collections.Generic;
using System.Linq;

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

class Program
{
    static void Main()
    {
        // 示例数据
        List<Person> people = new List<Person>
        {
            new Person { Name = "Alice", Age = 25 },
            new Person { Name = "Bob", Age = 30 },
            new Person { Name = "Charlie", Age = 35 }
        };

        // 投影操作,只选择姓名
        var names = from person in people
                    select person.Name;

        // 输出结果
        foreach (var name in names)
        {
            Console.WriteLine(name);
        }
    }
}

这个例子中,使用select person.NamePerson对象投影为姓名。

4. 分组操作

分组操作可以将数据按照某个键进行分组。

csharp

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        // 示例数据
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        // 按奇偶性分组
        var groups = from num in numbers
                     group num by num % 2;

        // 输出结果
        foreach (var group in groups)
        {
            Console.WriteLine($"Key: {group.Key}");
            foreach (var num in group)
            {
                Console.WriteLine(num);
            }
        }
    }
}

这里使用group num by num % 2将数字按奇偶性分组。

5. 连接操作

连接操作可以将两个数据源根据某个条件进行连接。

csharp

using System;
using System.Collections.Generic;
using System.Linq;

class Department
{
    public int Id { get; set; }
    public string Name { get; set; }
}

class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int DepartmentId { get; set; }
}

class Program
{
    static void Main()
    {
        // 示例数据
        List<Department> departments = new List<Department>
        {
            new Department { Id = 1, Name = "HR" },
            new Department { Id = 2, Name = "IT" }
        };

        List<Employee> employees = new List<Employee>
        {
            new Employee { Id = 1, Name = "Alice", DepartmentId = 1 },
            new Employee { Id = 2, Name = "Bob", DepartmentId = 2 }
        };

        // 连接操作
        var query = from emp in employees
                    join dept in departments on emp.DepartmentId equals dept.Id
                    select new { EmployeeName = emp.Name, DepartmentName = dept.Name };

        // 输出结果
        foreach (var result in query)
        {
            Console.WriteLine($"Employee: {result.EmployeeName}, Department: {result.DepartmentName}");
        }
    }
}

这个例子中,使用join关键字将EmployeeDepartment两个集合根据DepartmentId进行连接。

LINQ 提供了丰富的查询操作,能极大地简化数据处理和查询的代码,提高开发效率。