【问题标题】:EntityFramework multiple Group by queryEntityFramework 多个 Group by 查询
【发布时间】:2021-09-23 13:28:08
【问题描述】:

我想打印每天营业额最高的技术名称。我有订单和订单项目表。我想内部连接这两个表,并使用 Order 表中的“startdate”值和 orderitems 表中的“Quantity”和“finalPrice”。

[HttpPost]
public JsonResult MostGiroOfTechnologies(DateTime startDate, DateTime endDate)
{
    using (TridiContext context = new TridiContext())
    {
        var technologies = (from oi in context.OrderItems
            join or in context.Orders on oi.OrderId equals or.Id
            where (or.Status == 5 || or.Status == 7)
            && startDate < or.CompletedDate
            && or.CompletedDate < endDate
            group or by new { or.CompletedDate.Date ,oi.Technology} into g
            select new 
            {
                startDate = g.Key.Date,
                technology = g.Key.Technology
                //price = ?
            }
        ).OrderBy(o => o.startDate).ToList();
        return Json(new { technologies });
    }
}

当我按上述方式分组时,我得到了我想要的输出,但价格为空。当我将价格添加到分组依据时,输出比我想要的要多,因为它这次也按价格分组。

public class Order
{
    public int Id { get; set; }
    public int UserId { get; set; }
    public DateTime CreatedDate { get; set; }
    public DateTime CompletedDate { get; set; }
    public DateTime OrderEta { get; set; }
    public int Price { get; set; }
    public long? FinalPrice { get; set; }
    public int Discount { get; set; }
    public int Status { get; set; }
    public int SourceOfferId { get; set; }
}
public class OrderItems
{
    public int Id { get; set; }
    public int OrderId { get; set; }
    public string Technology { get; set; }
    public string Material { get; set; }
    public int Quantity { get; set; }
    public long? FinalPrice { get; set; }
}

【问题讨论】:

  • price应该如何计算?尝试使用g.Sum(...)
  • 您将 OrderItems 按 (..) 分组,因此在组内有它们的集合。当然价格不同,你必须决定哪个值是正确的。为什么订单有价格?
  • @GertArnold 我应该在 group by 中包含价格吗?使用 group by 这样做是否有意义,还是有其他更好的方法?
  • @benuto 当我在分组依据中包含价格时,具有相同技术的订单会出现在不同的行中,因为它也按价格分组。我想做的事。我会看每天的订单,我需要对订购产品的技术进行分组,得到偏差最大的技术。
  • @GertArnold ' g.Sum(p=>p.FinalPrice) ' 非常感谢朋友。

标签: c# sql entity-framework


【解决方案1】:

这段代码对我有用。

    [HttpPost]
    public JsonResult MostGiroOfTechnologies(DateTime startDate, DateTime endDate)
    {
        using (TridiContext context = new TridiContext())
        {
            var technologies = (from oi in context.OrderItems
                join or in context.Orders on oi.OrderId equals or.Id
                where (or.Status == 5 || or.Status == 7)
                && startDate < or.CompletedDate
                && or.CompletedDate < endDate
                group or by new { or.CompletedDate.Date ,oi.Technology} into g
                select new 
                {
                    startDate = g.Key.Date,
                    technology = g.Key.Technology
                    price = g.Sum(p=>p.FinalPrice)  //this line
                }
            ).OrderBy(o => o.startDate).ToList();
            return Json(new { technologies });
        }
    }

【讨论】:

    猜你喜欢
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多