对序列进行分类汇总,汇总后产生的序列的key就是按照某个字段汇总的项 。groupby select Viewmodel三者常一起使用,用于统计。groupby 方法语法常与select 子句形成数据 viewModel 的 数据投影。public ActionResult Index() { var context = new SiteDataContext(); var notifications = context.Notifications .GroupBy(n => n.NotificationType) .Select(g => new NotificationViewModel { Count = g.Count(), NotificationType = g.Key.ToString(), BadgeClass = NotificationType.Email == g.Key ? "success" : "info" }); ViewBag.Notifications = notifications; return View(); }
时间: 2024-09-29 00:20:32