where子句我有一個查詢應該返回本週總報告小時數的總和。 下面的代碼返回小時數的正確總數,但不返回數據庫中特定用戶的總數。Linq查詢,搞砸方法
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;
}
也許你應該設置用戶名到你的班級的屬性。我知道WP7 Linq是不是很好,傳遞方法 – 2012-04-20 11:38:58
的值,你需要使用加入http://code.msdn.microsoft.com/LINQ-Join-Operators-dabef4e9 – Habib 2012-04-20 11:41:35
傳遞用戶名到像這樣的方法適用於我使用的另一個linq查詢,但不適用於此特定的查詢。 – rickard 2012-04-20 11:42:17