我有两个表, picture
和pictureratings
在我的数据库。
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
一行将被创建。 我想组pictureratings
由图片ID表再算上喜欢。 我想告诉那些喜欢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),我得到一个异常说
演员阵容价值型“双师型”失败,因为物化值为null。 无论是结果型的泛型参数或查询必须使用可空类型。
如何去除错误?