我試着寫在LINQ此查詢LINQ的LEFT JOIN,如果計數爲0則返回1
SELECT dbo.Tbl_Users.ID, COUNT(dbo.Tbl_Inputs.ID) AS Inputs
FROM dbo.Tbl_Users LEFT JOIN
dbo.Tbl_Inputs ON dbo.Tbl_Inputs.User_ID = dbo.Tbl_Users.ID
GROUP BY dbo.Tbl_Users.ID
這是我的LINQ代碼,我做了一個類用戶()持有的用戶ID和的輸入中的計數Tbl_Inputs取得了用戶:
List<User> GetAllUsers()
{
List<User> userlist = new List<User>();
userlist = (from u in Data.Tbl_Users
join i in Data.Tbl_Inputs on u.ID equals i.User_ID
into Joined
from p in Joined.DefaultIfEmpty()
group p by new { u.ID }
into grp select new User()
{
Id = grp.Key.ID,
Inputs = grp.Count()
}).ToList();
return userlist;
}
的問題是,如果輸入爲0則返回1
不能弄清楚。
感謝
是否'User'具有導航屬性到'Tbl_Inputs'? – James