在C#的LINQ技术体系中,Count和Sum是两个核心的聚合方法,前者用于统计集合中满足条件的元素数量,后者用于计算集合中数值类型元素的总和,二者在数据处理场景中应用频率极高。

一、LINQ Count方法基础用法
Count方法有两种常用重载形式,一种是无参形式,直接统计集合中所有元素的数量;另一种是带谓词参数的形式,统计满足指定条件的元素数量。
1. 无参Count统计总元素数
当不需要筛选条件时,直接调用Count即可获取集合的元素总数,示例如下:
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
// 定义整数列表
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// 统计总元素数
int totalCount = numbers.Count();
Console.WriteLine($"列表总元素数量:{totalCount}"); // 输出:列表总元素数量:10
}
}
2. 带条件的Count统计
如果需要统计满足特定条件的元素数量,可以传入Func<T, bool>类型的谓词参数,示例如下:
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// 统计大于5的元素数量
int greaterThanFiveCount = numbers.Count(n => n > 5);
Console.WriteLine($"大于5的元素数量:{greaterThanFiveCount}"); // 输出:大于5的元素数量:5
}
}
二、LINQ Sum方法基础用法
Sum方法用于计算集合中所有数值类型元素的总和,同样支持无参和带转换参数的形式,也可以结合条件先筛选再求和。
1. 基础数值集合求和
对于整数、浮点数等数值类型的集合,直接调用Sum即可计算总和:
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
// 计算总和
int sum = numbers.Sum();
Console.WriteLine($"元素总和:{sum}"); // 输出:元素总和:15
}
}
2. 自定义对象集合求和
当集合元素是自定义对象时,需要指定求和的属性,示例如下:
using System;
using System.Collections.Generic;
using System.Linq;
// 定义订单类
public class Order
{
public int Id { get; set; }
public decimal Price { get; set; }
public int Count { get; set; }
}
class Program
{
static void Main()
{
// 初始化订单列表
List<Order> orders = new List<Order>
{
new Order { Id = 1, Price = 29.9m, Count = 2 },
new Order { Id = 2, Price = 49.9m, Count = 1 },
new Order { Id = 3, Price = 19.9m, Count = 3 }
};
// 计算所有订单的总金额(单价*数量)
decimal totalAmount = orders.Sum(o => o.Price * o.Count);
Console.WriteLine($"订单总金额:{totalAmount}"); // 输出:订单总金额:148.4
}
}
三、Count和Sum结合使用的实战案例
在实际业务中,经常需要同时统计数量和计算总和,以下是一个学生成绩统计的案例:
using System;
using System.Collections.Generic;
using System.Linq;
// 定义学生类
public class Student
{
public string Name { get; set; }
public int Score { get; set; }
}
class Program
{
static void Main()
{
List<Student> students = new List<Student>
{
new Student { Name = "张三", Score = 85 },
new Student { Name = "李四", Score = 92 },
new Student { Name = "王五", Score = 78 },
new Student { Name = "赵六", Score = 90 },
new Student { Name = "孙七", Score = 65 }
};
// 统计及格(分数>=60)的学生数量
int passCount = students.Count(s => s.Score >= 60);
// 计算所有学生的平均分(总分/总人数)
double averageScore = students.Sum(s => s.Score) / (double)students.Count();
// 计算及格学生的平均分
double passAverageScore = students.Where(s => s.Score >= 60).Sum(s => s.Score) / passCount;
Console.WriteLine($"及格学生数量:{passCount}");
Console.WriteLine($"全体学生平均分:{averageScore:F2}");
Console.WriteLine($"及格学生平均分:{passAverageScore:F2}");
}
}
四、使用注意事项
- Count方法如果传入条件谓词,会遍历整个集合统计满足条件的元素,大数据量场景下如果只需要判断是否存在元素,优先使用
Any方法性能更好。 - Sum方法如果集合为空,会返回对应数值类型的默认值,比如整数集合返回0,可空类型集合返回null,使用时需要注意空值处理。
- 对于实现了
ICollection<T>接口的集合,无参Count方法会直接读取集合的Count属性,不会遍历集合,性能优于遍历操作。
注意:LINQ的Count和Sum方法都属于延迟执行吗?实际上这两个方法都是立即执行的聚合方法,调用后会立刻遍历集合计算结果,不会等到枚举时才执行。