我有一个包含多个列的表:HarvestID,HarvestDate,UserID以提及主要的列。 对于每个日期,每天将有几个HarvestID。
到目前为止,我有以下linq查询:
TheUserID和TheMonth作为int和DateTime
传入var MyQuery = from h in MyDC.HarvestTable
where h.UserID == TheUserID
where h.HarvestDate.Month == TheMonth.Month
where h.HarvestDate.Year == TheMonth.Year
group h by h.HarvestDate.Day into TheDays
from d in TheDays
select new
{
TheDay = d.HarvestDate.Date,
TheDayCount = (from c in TheDay
select c.HarvestID).Count()
};
我希望输出是每天的计数列表。查询不会出错,但问题是查询当前没有为每一天返回唯一的行。分组不起作用,我找不到原因。这段代码出了什么问题?
感谢。
答案 0 :(得分:2)
var MyQuery = from h in MyDC.HarvestTable
where h.UserID == TheUserID
&& h.HarvestDate.Month == TheMonth.Month
&& h.HarvestDate.Year == TheMonth.Year
group h by h.HarvestDate.Day into g
select new
{
TheDay = g.Key,
TheDayCount = g.Count()
};
在没有数据的日子里,这不会给你零 - 但应该给你一个计数。
答案 1 :(得分:2)
它认为您的查询应该是
var MyQuery = from h in MyDC.HarvestTable
where h.UserID == TheUserID
where h.HarvestDate.Month == TheMonth.Month
where h.HarvestDate.Year == TheMonth.Year
group h by h.HarvestDate.Day into TheDays
select new
{
TheDay = TheDays.Key,
TheDayCount = TheDays.Count()
};
以上是对上述分组http://msdn.microsoft.com/en-us/vcsharp/aa336754.aspx#simple1
的非常好的参考