【问题标题】:Display the sum of the orders of the month in chart在图表中显示当月订单总和
【发布时间】:2017-03-08 00:55:28
【问题描述】:

我不知道如何显示,在asp.net MVC的表格中,开头月份的订单总和。 我在 Order 表中的数据如下所示:

创建日期 |总价
2017-02-06 | 400
2017-02-06 | 300
2017-03-06 | 100
2017-03-06 | 50

我想得到 TotalPrice 的总和,如下所示:

创建日期 |总价
2017-02 | 700
2017-03 | 150

我的模型类订单:

public int orderId { get; set; }
    public string UserId { get; set; }
    public virtual ApplicationUser User { get; set; }
    [StringLength(100)]
    public string firstName { get; set; }
    [StringLength(150)]
    public string lastName { get; set; }
    [StringLength(150)]
    public string address { get; set; }
    [StringLength(20)]
    public string phoneNumber { get; set; }
    public string email { get; set; }

    public string comment { get; set; }
    public DateTime dateCreated { get; set; }
    public OrderState orderState { get; set; }
    public decimal totalPrice { get; set; }

    public List<OrderIteam> OrderIteams { get; set; }

我尝试写这样的东西来在表格中显示当月订单的总和:

public ActionResult ListOrder()
{
    var result =
        from s in db.Orders
        group s by new { date = new DateTime(s.dateCreated.Year, s.dateCreated.Month, 1) } into g
        select new
        {
            dateCreated = g.Key.date,
            sumTotalPrice = g.Sum(x => x.totalPrice)
        };
    return View(result);
}

My view looks like this

我收到错误消息:

传入字典的模型项的类型为“System.Data.Entity.Infrastructure.DbQuery1[&lt;&gt;f__AnonymousType52[System.DateTime,System.Decimal]]”,但该字典需要一个“系统”类型的模型项.Collections.Generic.IEnumerable`1[Sklep_Internetowy.Models.Order]'。

最后我想将给定月份的订单总和如下图所示: Chart

【问题讨论】:

    标签: asp.net asp.net-mvc linq charts


    【解决方案1】:

    目前,下面的 LINQ 查询返回匿名类型的 DbQuery 实例而不是 IEnumerable 集合:

    var result = from s in db.Orders
                 group s by new { date = new DateTime(s.dateCreated.Year, s.dateCreated.Month, 1) } into g
                 select new
                 {
                     dateCreated = g.Key.date,
                     sumTotalPrice = g.Sum(x => x.totalPrice)
                 }; // returns System.Data.Entity.Infrastructure.DbQuery<AnonymousTypeN>
    

    这里抛出InvalidOperationException的原因仅仅是因为不同类型的数据被传递到需要IEnumerable&lt;Sklep_Internetowy.Models.Order&gt;的视图中。

    尝试将返回类型更改为Order,而不是使用匿名类型一:

    var result = from s in db.Orders
                 group s by new { date = new DateTime(s.dateCreated.Year, s.dateCreated.Month, 1) } into g
                 select new Order // return Sklep_Internetowy.Models.Order here
                 {
                     dateCreated = g.Key.date,
                     sumTotalPrice = g.Sum(x => x.totalPrice)
                 };
    

    此时,上面的 LINQ 查询的返回类型变为IQueryable&lt;Sklep_Internetowy.Models.Order&gt;,它实现了IEnumerable,因此不需要转换(但您可能仍需要use collection aggregate methods to execute it,例如ToList())。

    相关问题:

    The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery', but this dictionary requires a model item of type B

    The model item passed into the dictionary is 'System.Data.Entity.Infrastructure.DbQuery`, but requires 'System.Collections.Generic.IEnumerable

    【讨论】:

    • 我使用了您的代码,但现在出现错误:EntityFramework.SqlServer.dll 中发生“System.NotSupportedException”类型的异常,但未在用户代码中处理其他信息:仅无参数构造函数和初始化程序在 LINQ to Entities 中受支持。
    • 查询语句后返回视图之前是否使用了其他方法? NotSupportedException 表示查询执行后出现其他问题,因此包括抛出异常的代码行。如果你使用highcharts.js,也可以看看这个:nimbo.com/blog/how-to-use-highcharts-js-with-asp-net-mvc-4
    • 感谢@Tetsuya Yamamoto 的回答。构造带参数的日期。我猜是因为我有这个错误,但我不知道如何保存。
    • 可以展示触发NotSupportedException的数据模型类结构及相关查询处理代码。如果对模型类构造函数进行了彻底检查,则上述修改后的查询应该可以工作。
    • 感谢@Tetsuya Yamamoto。我的代码现在可以正常工作了。
    猜你喜欢
    • 1970-01-01
    • 2010-11-08
    • 2019-12-23
    • 1970-01-01
    • 2021-01-14
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多