You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按月份分组订单数据?解决int转DateTimeOffset类型转换错误

订单按月份分组的类型错误修复及需求实现

问题根源

  1. 类型不匹配:MyViewModel的Month字段定义为DateTimeOffset,但分组后拿到的g.Key.Month是int类型的月份数字,直接赋值导致转换错误。
  2. 返回类型矛盾:GetDataAsync声明返回IEnumerable<Order>,但实际返回的是List<MyViewModel>,类型完全不匹配。
  3. 需求偏差:需要的是英文月份名称(如January),但原代码试图用日期类型或数字来匹配。

修正后的完整代码

1. 调整ViewModel结构

把Month字段类型改为string,匹配期望的输出格式:

public class Order {
    public DateTimeOffset OrderDate { get; set; }
    public decimal Subtotal { get; set; }
}

public class MyViewModel
{
    public string Month { get; set; }
    public decimal Subtotal { get; set; }
}

2. 修正接口定义

让接口方法返回正确的ViewModel集合:

public interface IOrderService
{
    IEnumerable<MyViewModel> GetData();
    // 如果需要异步实现,改成:Task<IEnumerable<MyViewModel>> GetDataAsync();
}

3. 修复服务层逻辑

调整分组规则(按年+月分组避免跨年份合并),并把月份数字转成英文名称:

public class OrderService : RepositoryBase<Order>, IOrderService
{
    public OrderService(RepositoryContext repositoryContext) : base(repositoryContext)
    {
    }

    public IEnumerable<MyViewModel> GetData()
    {
        return FindAll()
            .OrderBy(o => o.OrderDate)
            // 按年份和月份分组,防止2023年1月和2024年1月被合并
            .GroupBy(o => new 
            { 
                o.OrderDate.Year, 
                o.OrderDate.Month 
            })
            .Select(g => new MyViewModel
            {
                // 将数字月份转为英文全称
                Month = new DateTime(g.Key.Year, g.Key.Month, 1)
                    .ToString("MMMM", System.Globalization.CultureInfo.InvariantCulture),
                Subtotal = g.Sum(x => x.Subtotal)
            })
            .ToList();
    }
}

异步版本(可选)

如果需要异步操作,修改如下:

public interface IOrderService
{
    Task<IEnumerable<MyViewModel>> GetDataAsync();
}

public class OrderService : RepositoryBase<Order>, IOrderService
{
    public OrderService(RepositoryContext repositoryContext) : base(repositoryContext)
    {
    }

    public async Task<IEnumerable<MyViewModel>> GetDataAsync()
    {
        var orders = await FindAll().OrderBy(o => o.OrderDate).ToListAsync();
        return orders
            .GroupBy(o => new 
            { 
                o.OrderDate.Year, 
                o.OrderDate.Month 
            })
            .Select(g => new MyViewModel
            {
                Month = new DateTime(g.Key.Year, g.Key.Month, 1)
                    .ToString("MMMM", System.Globalization.CultureInfo.InvariantCulture),
                Subtotal = g.Sum(x => x.Subtotal)
            })
            .ToList();
    }
}

效果验证

运行修正后的代码,会输出期望的结果:

[
  {"Month": "January", "Subtotal": 300},
  {"Month": "February", "Subtotal": 400},
  {"Month": "March", "Subtotal": 400},
  {"Month": "April", "Subtotal": 700}
]

内容的提问来源于stack exchange,提问作者Njabulo Mamba

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 13:30:15