如何使用 linq 获取 table 中的值计数
How to get the count of values in table using linq
我需要查询 db 以从我们网站的 highchart 小部件中获取数据,这是我目前使用的代码,
var highChartsData =
(from a in db.roomdetails
join b in db.ApplySchedule on a.RoomId equals b.RoomID
where b.Status == true
select new HighlinePie
{
Title = a.RoomName,
Date = b.MDate,
Value = db.ApplySchedule.Where(x => x.RoomID == a.RoomId).GroupBy(x=>x.MDate).Count(),
}).ToList();
这种方法的问题是现在获取总数,但我需要的是基于日期的计数,例如,如果日期 12/09/20201 有两个条目,而 14/09/ 有三个条目20201 数据应为“Title,12/09/20201,2”,“Title,14/09/20201,3”。
您必须使用分组:
var groupQuery =
from a in db.roomdetails
join b in db.ApplySchedule on a.RoomId equals b.RoomID
where b.Status == true
group b by new { a.RoomName, b.MDate } into g
select new HighlinePie
{
Title = g.Key.RoomName,
Date = g.Key.MDate,
Value = g.Count()
};
var highChartsData = groupQuery.ToList();
我需要查询 db 以从我们网站的 highchart 小部件中获取数据,这是我目前使用的代码,
var highChartsData =
(from a in db.roomdetails
join b in db.ApplySchedule on a.RoomId equals b.RoomID
where b.Status == true
select new HighlinePie
{
Title = a.RoomName,
Date = b.MDate,
Value = db.ApplySchedule.Where(x => x.RoomID == a.RoomId).GroupBy(x=>x.MDate).Count(),
}).ToList();
这种方法的问题是现在获取总数,但我需要的是基于日期的计数,例如,如果日期 12/09/20201 有两个条目,而 14/09/ 有三个条目20201 数据应为“Title,12/09/20201,2”,“Title,14/09/20201,3”。
您必须使用分组:
var groupQuery =
from a in db.roomdetails
join b in db.ApplySchedule on a.RoomId equals b.RoomID
where b.Status == true
group b by new { a.RoomName, b.MDate } into g
select new HighlinePie
{
Title = g.Key.RoomName,
Date = g.Key.MDate,
Value = g.Count()
};
var highChartsData = groupQuery.ToList();