我有一个查询,应该返回当前一周报告的总工时。下面的代码返回正确的总工时,但不是针对数据库中特定的用户。
public int reportedWeekTime(int currentWeek, string username)
{
var totalTime = (from u in context.Users
from r in context.Reports
from w in context.Weeks
from d in context.Days
where u.Id == r.UserId && r.weekNr.Equals(currentWeek) && r.Id == w.ReportId && w.DayId == d.Id
select d.Hour).DefaultIfEmpty(0).Sum();
return totalTime;
}
第一种方法返回数字24,这是正确的,但正如我所说,不适用于特定用户。
我正在尝试做到这一点,但返回值为0。我做错了什么?
public int reportedWeekTime(int currentWeek, string username)
{
var totalTime = (from u in context.Users
from r in context.Reports
from w in context.Weeks
from d in context.Days
where u.Id == r.UserId && r.weekNr.Equals(currentWeek) && r.Id == w.ReportId && w.DayId == d.Id && u.Username.Contains(username)
select d.Hour).DefaultIfEmpty(0).Sum();
return totalTime;
}
context.Users.Where(user => !user.Contains(username))
。 - Kevin GosseContains
方法是区分大小写的。 - Kevin Gosse