C#LINQ常用扩展语句

在 C# 中,LINQ 提供了许多扩展方法,这些方法定义在 System.Linq 命名空间中。以下是一些常用的 LINQ 扩展方法:

  1. Where - 过滤数据集合,返回满足条件的元素。

    var filteredItems = collection.Where(item => item.SomeProperty > 10);
  2. Select - 从数据集合中选择数据或创建新的投影。

    var projectedItems = collection.Select(item => new { item.SomeProperty, item.AnotherProperty });
  3. SelectMany - 将多个序列合并为一个序列。

    var flattenedItems = collection.SelectMany(item => item.SubItems);
  4. OrderBy / OrderByDescending - 对数据集合进行排序。

    var sortedItems = collection.OrderBy(item => item.SomeProperty);
    var sortedItemsDescending = collection.OrderByDescending(item => item.SomeProperty);
  5. ThenBy / ThenByDescending - 对已排序的数据集合进行次级排序。

    var sortedItems = collection.OrderBy(item => item.SomeProperty)
                                .ThenBy(item => item.AnotherProperty);
  6. GroupBy - 根据某个键选择器将数据集合分组。

    var groupedItems = collection.GroupBy(item => item.SomeProperty);
  7. Aggregate - 将数据集合中的元素组合起来,例如计算总和。

    int sum = collection.Aggregate((accumulated, current) => accumulated + current.SomeProperty);
  8. Count - 返回数据集合中的元素数量。

    int count = collection.Count();
  9. Any / All - 检查数据集合中是否至少有一个元素满足条件或所有元素都满足条件。

    bool hasItems = collection.Any();
    bool allMatch = collection.All(item => item.SomeProperty > 10);
  10. FirstOrDefault / SingleOrDefault - 返回数据集合中的第一个元素或唯一的元素,如果没有找到则返回默认值。

    T firstItem = collection.FirstOrDefault();
    T singleItem = collection.SingleOrDefault(item => item.SomeProperty == value);
  11. LastOrDefault / ElementAtOrDefault - 返回数据集合中的最后一个元素或指定索引的元素,如果没有找到则返回默认值。

    T lastItem = collection.LastOrDefault();
    T itemAt = collection.ElementAtOrDefault(index);
  12. Contains - 检查数据集合中是否包含特定的元素。

    bool contains = collection.Contains(item);
  13. DefaultIfEmpty - 如果数据集合为空,则返回包含默认值的集合。

    var nonEmptyCollection = collection.DefaultIfEmpty();
  14. Skip / Take - 跳过数据集合中的指定数量的元素或取指定数量的元素。

    var skippedItems = collection.Skip(count);
    var takenItems = collection.Take(count);
  15. Distinct - 返回数据集合中的唯一元素。

    var distinctItems = collection.Distinct();
  16. Union / Intersect / Except - 返回数据集合的并集、交集或差集。

    var unionItems = collection1.Union(collection2);
    var intersectItems = collection1.Intersect(collection2);
    var exceptItems = collection1.Except(collection2);
  17. Concat - 连接两个序列。

    var concatenatedCollections = collection1.Concat(collection2);
  18. Sum / Average / Min / Max - 计算数值数据集合的总和、平均值、最小值或最大值。

    int sum = collection.Sum(item => item.SomeIntegerProperty);
    double average = collection.Average(item => item.SomeDecimalProperty);
    T min = collection.Min(item => item.SomeProperty);
    T max = collection.Max(item => item.SomeProperty);

这些扩展方法是 LINQ 查询的基础,它们可以单独使用,也可以组合使用,以构建复杂的查询。使用这些方法时,需要确保已经添加了 using System.Linq; 命名空间引用。

  • 10
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值