【发布时间】:2013-09-25 16:06:31
【问题描述】:
我的数据库中有两个表,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 行。我想按图片 ID 对 pictureratings 表进行分组,然后计算点赞数。我想在totalrating 属性的picture 表中显示这些喜欢。
所以按照我现在的情况,我可以编写以下代码
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。结果类型的泛型参数或查询必须使用可为空的类型。
如何消除错误?
【问题讨论】:
标签: c# sql asp.net-mvc linq lambda