程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了EntityFramework 多个 Group by 查询大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决EntityFramework 多个 Group by 查询?

开发过程中遇到EntityFramework 多个 Group by 查询的问题如何解决?下面主要结合日常开发的经验,给出你关于EntityFramework 多个 Group by 查询的解决方法建议,希望对你解决EntityFramework 多个 Group by 查询有所启发或帮助;

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

[httpPost]
public JsonResult MostGiroOfTechnologIEs(datetiR_956_11845@e startDate,datetiR_956_11845@e 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 });
    }
}
@H_502_4@

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

public class Order
{
    public int id { get; set; }
    public int UserID { get; set; }
    public datetiR_956_11845@e CreatedDate { get; set; }
    public datetiR_956_11845@e CompletedDate { get; set; }
    public datetiR_956_11845@e 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; }
}
@H_502_4@

解决方法

这段代码对我有用。

    [httpPost]
    public JsonResult MostGiroOfTechnologies(datetiR_956_11845@e startDate,datetiR_956_11845@e 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 });
        }
    }

大佬总结

以上是大佬教程为你收集整理的EntityFramework 多个 Group by 查询全部内容,希望文章能够帮你解决EntityFramework 多个 Group by 查询所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。