我有两个表,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) 时,我得到一个例外说
转换为值类型“Double”失败,因为具体化值为 null。结果类型的泛型参数或查询必须使用可为空的类型。
如何消除错误?