根据既定报表契约审查这个 LINQ 辅助方法。
调用方数据只能枚举一次;超过 100,000 笔订单时拒绝处理;汇总包含负数退款;客户名称按大小写不敏感规则分组;返回已物化的前十项,先按总额降序,再按名称排序。
csharp
using System;
using System.Collections.Generic;
using System.Linq;
public sealed record Order(string Customer, decimal Amount);
public sealed record CustomerTotal(string Customer, decimal Amount);
public static class Reports
{
public static IReadOnlyList<CustomerTotal> Get(IEnumerable<Order> orders)
{
if (!orders.Any()) return [];
return orders
.Where(order => order.Amount > 0m)
.GroupBy(order => order.Customer)
.Select(group => new CustomerTotal(
group.Key,
group.Sum(order => order.Amount)))
.OrderByDescending(total => total.Amount)
.ThenBy(total => total.Customer, StringComparer.OrdinalIgnoreCase)
.Take(10)
.ToList();
}
}
生成代码仅作示例,不代表任何特定模型