[英]LINQ query to get records for the last 30 days
I have a LINQ query to find records for the last 7 days which works fine.我有一个 LINQ 查询来查找过去 7 天的记录,效果很好。 Although if I try use the same query but for the last 30 days then 0 is returned.
尽管如果我尝试使用相同的查询,但在过去 30 天内返回 0。 Can anyone tell me why this is?
谁能告诉我这是为什么? And how can I edit the query to work for the last 30 days?
我如何编辑查询以在过去 30 天内工作?
Works fine:工作正常:
//calories burned working out over the last 7 days
public int CaloriesBurnedLast7Days
{
get
{
using (var db = new FitnessTrackerWebAPIContext())
{
int calsLast7Days = 0;
calsLast7Days = db.Workouts.Where(w => w.Date <= DateTime.Now.AddDays(-7) && w.UserID == ID).Sum(w => w.CaloriesBurned);
return calsLast7Days;
}
}
}
Does not work correctly:不能正常工作:
//calories burned working out over the last 30 days
public int CaloriesBurnedLast30Days
{
get
{
using (var db = new FitnessTrackerWebAPIContext())
{
int calsLast30Days = 0;
calsLast30Days = db.Workouts.Where(w => w.Date <= DateTime.Now.AddDays(-30) && w.UserID == ID).Sum(w => w.CaloriesBurned);
return calsLast30Days;
}
}
}
I have a LINQ query to find records for the last 7 days which works fine
我有一个 LINQ 查询来查找过去 7 天的记录,效果很好
No, you have a query that returns the calories burned more than 7 days ago (ie 8, 9, 10, etc).不,您有一个查询返回超过 7 天前(即 8、9、10 等)燃烧的卡路里。 The way you structured your query, the correct way would be
w.Date >= DateTime.Now.AddDays(-n)
to get the last n
days.您构建查询的方式,正确的方法是
w.Date >= DateTime.Now.AddDays(-n)
以获取最后n
天。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.