微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在实体框架和ASP.NET MVC中使用groupby子句并获取计数

如何解决如何在实体框架和ASP.NET MVC中使用groupby子句并获取计数

我有一个聊天模型类,如下:

public class Chat
    {
        [Key]
        public int ChatId { get; set; }

        [required]
        public string CustName { get; set; }


        public string Query { get; set; }

        public string Resolution { get; set; }

        public string Region { get; set; }

        [required]
        public DateTime ChatStartDateTime { get; set; }

        public DateTime? ChatCreateDateTime { get; set; }

        public DateTime? ChatEndDateTime { get; set; }

        public string Feedback { get; set; }

        public DateTime? FeedbackDateTime { get; set; }

        public string FeedbacksharedBy { get; set; }

        public int Id { get; set; }
        public string Username { get; set; }
        public string FirstName { get; set; }
        public string Email { get; set; }
        public string DidCustCreateTicket { get; set; }

        [EmailAddress(ErrorMessage = "Invalid Email Address")]
        public string Custemail { get; set; }

        public int? ScenarioId { get; set; }
        public string ScenarioList { get; set; }


        [ForeignKey("Id")]
        public virtual User User { get; set; }

        [ForeignKey("ScenarioId")]
        public virtual Scenario Scenario { get; set; }

        public IEnumerable<SelectListItem> Scenarios {get;set;}

        public IEnumerable<SelectListItem> Users { get; set; }

    }

我想获取“聊天”报告,以便在“工具”中添加了多少个聊天,并按“情景”将其分组,并仅显示“情景总计数”。

例如:

TOtal Chats Count groupby Scenario

这是我尝试过但不知道自己在做什么的查询

public ActionResult test()
        {
            using (Db db = new Db())
            {
                ViewBag.resultset = db.Chats
            .Where(x => System.Data.Entity.DbFunctions.TruncateTime(x.ChatCreateDateTime) == DateTime.Today)
            .GroupBy(ddda => ddda.Scenario)
            .ToList()
            .Select(s => new
            {
                //month = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(s.Key),count = s.Count()
            });

                return View();
            }
        }

非常感谢您的帮助

解决方法

通过创建另一个具有2个已设置属性ScenarioName和Count的VM类作为ScenarioVM来解决该问题,并将其用于实际的查询中。让我知道是否有人需要帮助

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。