WPF 数据查询:使用 LINQ 进行高效操作
WPF 可以使用 LINQ(Language-Integrated Query,语言集成查询)进行数据查询和操作。以下是一个简单的例子:
假设有一个 Student 类,包含属性 Name 和 Age:
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
}
现在有一个 List
List<Student> students = new List<Student>()
{
new Student() { Name = '张三', Age = 20 },
new Student() { Name = '李四', Age = 21 },
new Student() { Name = '王五', Age = 22 },
new Student() { Name = '赵六', Age = 23 },
};
// 查询年龄大于等于22的学生
var result = from s in students
where s.Age >= 22
select s;
foreach (var student in result)
{
Console.WriteLine(student.Name);
}
这里使用了 LINQ 的查询表达式语法,其中 from 关键字指定要查询的数据源(这里是 students),where 关键字指定查询条件,select 关键字指定查询结果。通过 foreach 循环输出查询结果。
除了查询,LINQ 还支持排序、分组、投影等操作。具体使用方法可以参考 MSDN 文档。
原文地址: https://www.cveoy.top/t/topic/mg1L 著作权归作者所有。请勿转载和采集!