SQLite无法对“decimal”类型的表达式应用聚合运算符“Sum”,使用LINQ to Objects在客户端聚合结果

vpfxa7rd  于 12个月前  发布在  SQLite
关注(0)|答案(4)|浏览(342)

运行下面的代码,OrdersPrice=g. Sum (p=>p.TotalPrice)行出现错误,为什么?我想查询订单对应的经理最近一个月的销售数据,按ManagerId分组,统计订单数和销售总额,一个Order对应一个订单,TotalPrice是一个订单的金额。
System.NotSupportedException:SQLite无法对“decimal”类型的表达式应用聚合运算符“Sum”。请将值转换为支持的类型,或使用LINQ to Objects在客户端聚合结果。
我代码:

[HttpGet]
public async Task<ActionResult<IEnumerable<ManagerAnalysis>>> GetManagerAnalysis()
{
    if (_context.Orders == null)
    {
        return NotFound();
    }
    var analysis = from Order in _context.Orders
                   where Order.OrderTime > DateTime.Now.AddMonths(-1)
                   group Order by Order.Manager.Id into g
                   select new ManagerAnalysis
                   {
                       ManagerName = g.First().Manager.Name,
                       OrderCount = g.Count(),
                       OrdersPrice = g.Sum(p => p.TotalPrice),//The error line.
                   };
    return await analysis.ToListAsync();
}
public class ManagerAnalysis
 {
     public string? ManagerName { get; set; }
     public int OrderCount { get; set; }
     public decimal OrdersPrice { get; set; } = 0;
 }
public class Order
{
    public ulong Id { get; set; }

    public Manager Manager { get; set; } = new Manager();

    public decimal TotalPrice { get; set; }
 
    public DateTime OrderTime { get; set; }
}

字符串
我试过OrdersPrice = g.Sum(p => (decimal)p.TotalPrice)OrdersPrice = (decimal)g.Sum(p => p.TotalPrice),但是都没有效果,我该怎么做?

v1uwarro

v1uwarro1#

SQLLite本身不支持decimal数据。一种解决方法是使用double进行SQL聚合,然后在单独的查询中转换为decimal

var analysisRaw = from Order in _context.Orders
               where Order.OrderTime > DateTime.Now.AddMonths(-1)
               group Order by Order.Manager.Id into g
               select new 
               {
                   ManagerName = g.First().Manager.Name,
                   OrderCount = g.Count(),
                   OrdersPrice = g.Sum(p => p.TotalPrice)
               };

var analysis = from Order in analysisRaw.AsEnumerable()
               select new ManagerAnalysis
               {
                   ManagerName = Order.ManagerName,
                   OrderCount = Order.OrderCount,
                   OrdersPrice = Convert.ToDecimal(Order.OrdersPrice))
               };

字符串

wfsdck30

wfsdck302#

SQLLite本身不支持十进制数据。使用double进行SQL聚合,然后在单独的查询中转换为十进制:

OrdersPrice = (decimal)g.Sum(p => (double)p.TotalPrice)

字符串

2w3kk1z5

2w3kk1z53#

你试过:
OrdersPrice =(decimal)g.Sum(p => decimal.ToDouble(p.TotalPrice));

xqkwcwgp

xqkwcwgp4#

你得到的错误是因为SQLite,你正在使用的数据库,不支持直接在查询中对十进制类型的Sum操作。这个问题是由于实体框架试图将你的LINQ查询转换为SQL,由于SQLite对小数的限制,它无法执行。解决方法是首先将LINQ查询转换为列表(执行查询并将数据提取到内存中),然后执行Sum操作。
你可以试试下面的代码:

[HttpGet]
public async Task<ActionResult<IEnumerable<ManagerAnalysis>>> GetManagerAnalysis()
{
    if (_context.Orders == null)
    {
        return NotFound();
    }

    // First, fetch the necessary data into memory
    var ordersInMemory = await _context.Orders
        .Where(order => order.OrderTime > DateTime.Now.AddMonths(-1))
        .ToListAsync();

    // Then perform the grouping and summing in memory
    var analysis = ordersInMemory
        .GroupBy(order => order.Manager.Id)
        .Select(g => new ManagerAnalysis
        {
            ManagerName = g.First().Manager.Name,
            OrderCount = g.Count(),
            OrdersPrice = g.Sum(p => p.TotalPrice) // Now this operation is performed in memory
        });

    return analysis;
}

字符串

相关问题