我的数据库中有一张图片表。对于每个新评级,将创建新的图片评级表行。我按上周和pictureid对这些评分进行分组,这意味着上周一张图片的所有评分都会累积并成为一个评级。然后我就可以按照他们的数量对它们进行排序。
这是我的图片表:
public partial class picturerating
{
public int idpicturerating { get; set; }
public int idpictures { get; set; }
public int iduser { get; set; }
public System.DateTime iddatetime { get; set; }
public int iduserratedby { get; set; }
public int rating { get; set; }
}
这是我的代码:
public IQueryable<SortedDto> GetRatignsAndTheirDetails()
{
var pastDate = DateTime.Now.Date.AddDays(-7);
var combo = from p in db.picturerating
where p.iddatetime > pastDate
group p by p.idpictures into pgroup
let count = pgroup.Count()
orderby count descending
select new SortedDto
{
IdPictures = pgroup.FirstOrDefault().idpictures,
IdPicturerating = pgroup.FirstOrDefault().idpicturerating,
IdUser = pgroup.FirstOrDefault().iduser,
IdDatetime = pgroup.FirstOrDefault().iddatetime,
IdUserratedBy = pgroup.FirstOrDefault().iduserratedby,
};
return combo;
}
现在我的问题出现了:
如何加入另外两个表,用户和picturedetails表来显示用户名和总评分。
目前我能够编写此代码,这只是一个粗略的想法,在我看来不是一个正确的代码,所以我需要帮助来更改此代码,使其工作:
var pastDate = DateTime.Now.Date.AddDays(-1);
var combo = from p in db.picturedetails
join u in db.users on p.iduser equals u.iduser
join l in db.pictureratings where l.iddatetime > pastDate group l by l.idpictures into pgroup
let count = pgroup.Count()
orderby count descending
select new SortedDto
{
IdPictures = pgroup.FirstOrDefault().idpictures,
IdPictureRating = pgroup.FirstOrDefault().idpicturerating,
IdUser = pgroup.FirstOrDefault().iduser,
IdDatetime = pgroup.FirstOrDefault().iddatetime,
IdUserRatedBy = pgroup.FirstOrDefault().iduserrateddby,
username=u.username,
dob=u.dob,
sex=u.sex,
totalrating = pl.Average(l => (float?)l.rating)?? 0 , // pl.Where(a => a.ratenumber!= null).Average(c => c.ratenumber)
};
return combo;
我做得对,有什么办法,如何更改上面的代码,使其按照我的需要正常工作。