随笔分类 - LINQ
摘要:Distinct 和 DistinctBy 以下示例演示字符串序列上 Enumerable.Distinct 方法的行为。 返回的序列包含输入序列的唯一元素。 string[] planets = { "Mercury", "Venus", "Venus", "Earth", "Mars", "Ea
阅读全文
摘要:一.查询语法 编写大多数查询的推荐方式是使用查询语法创建查询表达式。 // Query #1. List<int> numbers = new List<int>() { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; // The query variable can also b
阅读全文
摘要:语言集成查询 (LINQ) 是一系列直接将查询功能集成到 C# 语言的技术统称。 数据查询历来都表示为简单的字符串,没有编译时类型检查或 IntelliSense 支持。 此外,需要针对每种类型的数据源了解不同的查询语言:SQL 数据库、XML 文档、各种 Web 服务等。 借助 LINQ,查询成为
阅读全文
摘要:*或缺省方法的默认值 方法返回序列的第一个元素,如果未找到任何元素,则返回默认值。在 .NET 6 中,可以重写默认值。您也可以覆盖SingleOrDefault 和 LastOrDefault方法的默认值。 List<int> list1 = new() { 1, 2, 3 }; int item
阅读全文