diff --git "a/\351\273\216\346\254\243\346\254\243/20241211--Linq\346\237\245\350\257\242\343\200\201Lambda\350\241\250\350\276\276\345\274\217.md" "b/\351\273\216\346\254\243\346\254\243/20241211--Linq\346\237\245\350\257\242\343\200\201Lambda\350\241\250\350\276\276\345\274\217.md" new file mode 100644 index 0000000000000000000000000000000000000000..a6ce0671f2b6717a1daf7484da5d1cad7344575f --- /dev/null +++ "b/\351\273\216\346\254\243\346\254\243/20241211--Linq\346\237\245\350\257\242\343\200\201Lambda\350\241\250\350\276\276\345\274\217.md" @@ -0,0 +1,145 @@ +### LINQ查询方法 + +1. **Where** - 过滤元素 + ```csharp + var filtered = source.Where(item => item.SomeProperty == value); + // 筛选出source中所有SomeProperty等于value的元素 + ``` + +2. **Select** - 选择元素 + ```csharp + var selected = source.Select(item => item.SomeProperty); + // 从source中选择每个元素的SomeProperty属性 + ``` + +3. **SelectMany** - 展开序列 + ```csharp + var expanded = source.SelectMany(item => item.SubItems); + // 将source中的每个元素的SubItems集合展开成一个平面集合 + ``` + +4. **OrderBy / OrderByDescending** - 排序元素 + ```csharp + var ordered = source.OrderBy(item => item.SomeProperty); + // 按SomeProperty属性对source中的元素进行升序排序 + var orderedDescending = source.OrderByDescending(item => item.SomeProperty); + // 按SomeProperty属性对source中的元素进行降序排序 + ``` + +5. **ThenBy / ThenByDescending** - 次级排序 + ```csharp + var thenOrdered = ordered.ThenBy(item => item.AnotherProperty); + // 在已排序的ordered基础上,按AnotherProperty属性进行次级升序排序 + var thenOrderedDescending = orderedDescending.ThenByDescending(item => item.AnotherProperty); + // 在已排序的orderedDescending基础上,按AnotherProperty属性进行次级降序排序 + ``` + +6. **GroupBy** - 分组元素 + ```csharp + var grouped = source.GroupBy(item => item.SomeProperty); + // 根据SomeProperty属性的值对source中的元素进行分组 + ``` + +7. **All / Any / Contains** - 检查元素 + ```csharp + bool allMatch = source.All(item => item.SomeProperty == value); + // 检查source中所有元素的SomeProperty是否都等于value + bool anyMatch = source.Any(item => item.SomeProperty == value); + // 检查source中是否有元素的SomeProperty等于value + bool contains = source.Contains(item); + // 检查source中是否包含特定的item元素 + ``` + +8. **Count / LongCount** - 计数元素 + ```csharp + int count = source.Count(item => item.SomeProperty == value); + // 计算source中SomeProperty等于value的元素数量 + long longCount = source.LongCount(item => item.SomeProperty == value); + // 同Count,但是返回long类型,用于可能超过Int32.MaxValue的计数 + ``` + +9. **Min / Max** - 找到最小/最大值 + ```csharp + int minValue = source.Min(item => item.SomeProperty); + // 找出source中SomeProperty的最小值 + int maxValue = source.Max(item => item.SomeProperty); + // 找出source中SomeProperty的最大值 + ``` + +10. **Sum** - 求和 + ```csharp + int sum = source.Sum(item => item.SomeProperty); + // 计算source中所有元素的SomeProperty属性值的总和 + ``` + +11. **Average** - 求平均值 + ```csharp + double average = source.Average(item => item.SomeProperty); + // 计算source中所有元素的SomeProperty属性值的平均值 + ``` + +12. **Distinct / DistinctBy** - 去重 + ```csharp + var distinct = source.Distinct(); + // 移除source中的重复元素(基于Equals和GetHashCode) + var distinctByProperty = source.DistinctBy(item => item.SomeProperty); + // 根据SomeProperty属性移除source中的重复元素 + ``` + +13. **First / FirstOrDefault** - 获取第一个元素 + ```csharp + var first = source.First(item => item.SomeProperty == value); + // 获取source中第一个SomeProperty等于value的元素 + var firstOrDefault = source.FirstOrDefault(item => item.SomeProperty == value); + // 获取source中第一个SomeProperty等于value的元素,如果不存在则返回默认值 + ``` + +14. **Last / LastOrDefault** - 获取最后一个元素 + ```csharp + var last = source.Last(item => item.SomeProperty == value); + // 获取source中最后一个SomeProperty等于value的元素 + var lastOrDefault = source.LastOrDefault(item => item.SomeProperty == value); + // 获取source中最后一个SomeProperty等于value的元素,如果不存在则返回默认值 + ``` + +15. **ElementAt / ElementAtOrDefault** - 获取特定位置的元素 + ```csharp + var elementAt = source.ElementAt(index); + // 获取source中位于index位置的元素 + var elementAtOrDefault = source.ElementAtOrDefault(index); + // 获取source中位于index位置的元素,如果index超出范围则返回默认值 + ``` + +16. **DefaultIfEmpty** - 提供默认值 + ```csharp + var defaultIfEmpty = source.DefaultIfEmpty(defaultValue); + // 如果source为空,则返回包含defaultValue的集合,否则返回source + ``` + +17. **Skip / SkipWhile** - 跳过元素 + ```csharp + var skip = source.Skip(count); + // 跳过source中的前count个元素 + var skipWhile = source.SkipWhile(item => item.SomeProperty < value); + // 跳过source中直到第一个SomeProperty不小于value的元素 + ``` + +18. **Take / TakeWhile** - 取元素 + ```csharp + var take = source.Take(count); + // 取source中的前count个元素 + var takeWhile = source.TakeWhile(item => item.SomeProperty < value); + // 取source中直到第一个SomeProperty不小于value的元素 + ``` + +19. **Join** - 连接两个序列 + ```csharp + var join = source1.Join(source2, item1 => item1.Id, item2 => item2.ForeignId, (item1, item2) => new { item1, item2 }); + // 根据键选择器将source1和source2中的元素连接起来 + ``` + +20. **GroupJoin** - 将两个序列进行分组连接 + ```csharp + var groupJoin = source1.GroupJoin(source2, item1 => item1.Id, item2 => item2.ForeignId, (item1, items) => new { item1, items }); + // 将source1中的每个元素与source2中所有匹配的元素分组连接起来 + ``` \ No newline at end of file diff --git "a/\351\273\216\346\254\243\346\254\243/20241213--Linq\346\237\245\350\257\242\343\200\201Lambda\350\241\250\350\276\276\345\274\217.md" "b/\351\273\216\346\254\243\346\254\243/20241213--Linq\346\237\245\350\257\242\343\200\201Lambda\350\241\250\350\276\276\345\274\217.md" new file mode 100644 index 0000000000000000000000000000000000000000..ba56ad1856d8c9a2cf7c9f45bdd0bb05373c451f --- /dev/null +++ "b/\351\273\216\346\254\243\346\254\243/20241213--Linq\346\237\245\350\257\242\343\200\201Lambda\350\241\250\350\276\276\345\274\217.md" @@ -0,0 +1,115 @@ +### 额外的LINQ查询方法及详细讲解 + +1. **Aggregate** - 执行累加操作 + ```csharp + int sum = numbers.Aggregate((accumulate, current) => accumulate + current); + // 从numbers集合中累加所有数字,返回总和 + ``` + +. **AsEnumerable** - 提供LINQ操作的枚举 + ```csharp + IEnumerable enumerable = list.AsEnumerable(); + // 将list转换为可以进行LINQ操作的枚举 + ``` + +1. **Concat** - 连接两个序列 + ```csharp + IEnumerable concatenated = first.Concat(second); + // 将first和second两个序列连接起来,返回一个新的序列 + ``` + +4. **Zip** - 合并两个序列 + ```csharp + var zipped = first.Zip(second, (firstItem, secondItem) => new { firstItem, secondItem }); + // 将first和second两个序列中的元素配对,返回一个新的序列 + ``` + +5. **Union** - 返回两个序列的并集 + ```csharp + var union = first.Union(second); + // 返回first和second两个序列的并集,自动去除重复项 + ``` + +6. **Intersect** - 返回两个序列的交集 + ```csharp + var intersection = first.Intersect(second); + // 返回first和second两个序列共有的元素 + ``` + +7. **Except** - 返回两个序列的差集 + ```csharp + var difference = first.Except(second); + // 返回存在于first但不在second中的元素 + ``` + +8. **Reverse** - 反转序列 + ```csharp + var reversed = source.Reverse(); + // 返回source序列的反转版本 + ``` + +9. **SequenceEqual** - 比较两个序列是否相等 + ```csharp + bool areEqual = first.SequenceEqual(second); + // 检查first和second两个序列是否包含相同的元素,且顺序相同 + ``` + +10. **ToArray** - 转换为数组 + ```csharp + int[] array = source.ToArray(); + // 将source序列转换为数组 + ``` + +11. **ToList** - 转换为列表 + ```csharp + List list = source.ToList(); + // 将source序列转换为List + ``` + +12. **ToDictionary** - 转换为字典 + ```csharp + Dictionary dictionary = source.ToDictionary(item => item.Id, item => item.Name); + // 将source序列转换为字典,指定键和值的选择器 + ``` + +13. **ToLookup** - 转换为查找表 + ```csharp + ILookup lookup = source.ToLookup(item => item.Id, item => item.Name); + // 将source序列转换为查找表,可以按键查找多个值 + ``` + +14. **OfType** - 筛选特定类型的元素 + ```csharp + IEnumerable derivedItems = source.OfType(); + // 从source中筛选出Derived类型的对象 + ``` + +15. **Cast** - 转换元素类型 + ```csharp + IEnumerable derivedItems = source.Cast(); + // 将source中的元素转换为Derived类型 + ``` + +16. **OfType / Cast** - 处理异构对象集合 + ```csharp + IEnumerable derivedItems = source.OfType(); + // 从source中筛选出Derived类型的对象,适用于源序列类型不确定的情况 + ``` + +17. **Empty** - 创建空序列 + ```csharp + IEnumerable empty = Enumerable.Empty(); + // 创建一个空的int序列 + ``` + +18. **Range** - 生成数字范围 + ```csharp + IEnumerable numbers = Enumerable.Range(1, 10); + // 创建一个包含从1到10的整数序列 + ``` + +19. **Repeat** - 重复元素 + ```csharp + IEnumerable repeated = Enumerable.Repeat(1, 5); + // 创建一个包含5个1的序列 + ```