LINQ 和 Lambda 表达式的区别及示例(多个数组关联、分组、排序)
1. 概念区别
- LINQ (Language Integrated Query):是一种查询语法,允许你以声明式的方式编写查询表达式。它主要用于查询集合、数据库等数据源。
- Lambda 表达式:是一种匿名函数,可以作为参数传递给方法或存储在委托中。Lambda 表达式通常用于简化代码,使代码更加简洁。
2. 使用场景区别
- LINQ:主要用于查询操作,如过滤、排序、分组等。
- Lambda 表达式:不仅可以用于查询操作,还可以用于事件处理、线程启动、集合操作等更广泛的应用场景。
3. 语法区别
- LINQ 查询语法:使用类似 SQL 的语法结构。
- Lambda 表达式:使用
=>
符号定义,通常与方法调用结合使用。
4. 示例对比
4.1 多个数组关联、分组、排序
假设我们有两个数组:
students
:包含学生信息(ID 和姓名)scores
:包含学生成绩(学生ID 和分数)
我们将这两个数组进行关联,并按学生的总分进行分组和排序。
4.1.1 使用 LINQ 查询语法
csharp
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
// 定义两个数组
List<Student> students = new List<Student>
{
new Student { Id = 1, Name = "Alice" },
new Student { Id = 2, Name = "Bob" },
new Student { Id = 3, Name = "Charlie" }
};
List<Score> scores = new List<Score>
{
new Score { StudentId = 1, ScoreValue = 85 },
new Score { StudentId = 1, ScoreValue = 90 },
new Score { StudentId = 2, ScoreValue = 78 },
new Score { StudentId = 3, ScoreValue = 92 },
new Score { StudentId = 3, ScoreValue = 88 }
};
// 使用 LINQ 查询语法
var query = from s in students
join sc in scores on s.Id equals sc.StudentId into groupedScores
select new
{
StudentName = s.Name,
TotalScore = groupedScores.Sum(sc => sc.ScoreValue),
Scores = groupedScores.ToList()
};
// 按总分降序排序
var sortedQuery = from q in query
orderby q.TotalScore descending
select q;
Console.WriteLine("Results using LINQ query syntax:");
foreach (var result in sortedQuery)
{
Console.WriteLine($"{result.StudentName}: Total Score = {result.TotalScore}");
foreach (var score in result.Scores)
{
Console.WriteLine($" - {score.ScoreValue}");
}
}
}
}
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Score
{
public int StudentId { get; set; }
public int ScoreValue { get; set; }
}
4.1.2 使用 Lambda 表达式
csharp
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
// 定义两个数组
List<Student> students = new List<Student>
{
new Student { Id = 1, Name = "Alice" },
new Student { Id = 2, Name = "Bob" },
new Student { Id = 3, Name = "Charlie" }
};
List<Score> scores = new List<Score>
{
new Score { StudentId = 1, ScoreValue = 85 },
new Score { StudentId = 1, ScoreValue = 90 },
new Score { StudentId = 2, ScoreValue = 78 },
new Score { StudentId = 3, ScoreValue = 92 },
new Score { StudentId = 3, ScoreValue = 88 }
};
// 使用 Lambda 表达式
var query = students
.GroupJoin(
scores,
student => student.Id,
score => score.StudentId,
(student, groupedScores) => new
{
StudentName = student.Name,
TotalScore = groupedScores.Sum(score => score.ScoreValue),
Scores = groupedScores.ToList()
}
);
// 按总分降序排序
var sortedQuery = query.OrderByDescending(result => result.TotalScore);
Console.WriteLine("Results using Lambda expression:");
foreach (var result in sortedQuery)
{
Console.WriteLine($"{result.StudentName}: Total Score = {result.TotalScore}");
foreach (var score in result.Scores)
{
Console.WriteLine($" - {score.ScoreValue}");
}
}
}
}
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Score
{
public int StudentId { get; set; }
public int ScoreValue { get; set; }
}
5. 总结
- LINQ 查询语法 更加直观,适合复杂的查询逻辑,尤其是对于熟悉 SQL 的开发者。
- Lambda 表达式 更加简洁,适合简单的查询和操作,并且可以在更多场景下使用。