WPF的查询怎么写
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/W23 著作权归作者所有。请勿转载和采集!