我在我的數據庫中有兩個表,picture
和pictureratings
。當使用平均值時,linq查詢出錯
public partial class picture
{
public int idpicture { get; set; }
public int iduser { get; set; }
public string picTitle { get; set; }
public string picFilename { get; set; }
public System.DateTime pictime { get; set; }
public int nuditylevel { get; set; }
public int fakeslevel { get; set; }
// This property will hold the total accumulated/summed
// up rating of a picture
public int totalrating { get; set; }
}
public partial class pictureratings
{
public int idrating { get; set; }
public int idpictures { get; set; }
public int iduser { get; set; }
public System.DateTime iddatetime { get; set; }
public int iduserrateddby { get; set; }
public int rating { get; set; }
}
對於每個評級,將創建一個新的pictureratings
行。我想通過圖片ID將pictureratings
表分組,然後計算喜歡的。我想在picture
表中顯示那些喜歡totalrating
屬性的表。
所以,按我現在我能寫出下面的代碼
var combo = from p in db.picturedetails
join l in db.picturelikes on p.idpictures equals l.idpictures into pl
select new LikesandPictureDetails
{
IdUserPic = p.iduser,
IdPicturess =p.idpictures,
Likes = p.likes,
NudityLevel = p.nuditylevel,
PicTitle = p.picTitle,
PicTime = p.pictime,
picFilename=p.picFilename,
Count=pl.Count(),
totalrating = pl.Average(c => c.likenumber) // This line is causing error
};
我使用的Web API返回的查詢總數。我正在顯示picture
屬性,如iduser
,picTitle
,picFilename
,pictime
,nuditylevel
和fakeslevel
。
按現在每一件事情運行平穩,但是當我添加totalrating = pl.Average(C => c.likenumber),我得到一個異常說
演員陣容價值型「雙」失敗因爲物化值爲空。結果類型的泛型參數或查詢都必須使用可爲空的類型。
如何清除錯誤?
可能重複... 。面臨麻煩](http://stackoverflow.com/questions/19007164/using-linq-to-join-group-by-and-count-facing-a-trouble) –