我的数据库中有一张图片表。对于每个新评级,将创建一个新的图片评级表行。我将这些评级按上周和图片进行分组,这意味着上周一张图片的所有评级将被累积并成为一个评级。然后我将能够按他们的计数对它们进行排序。
这是我的画桌:
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;
}
现在我的问题来了:
如何加入另外两个表,用户和图片详细信息表以显示用户名和总评分。
根据现在我能够编写此代码,这只是我脑海中的一个粗略想法,而不是正确的代码,因此我需要帮助来更改此代码以使其工作:
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;
我做得对吗,怎么做,如何更改上面的代码以使其根据我的需要正常工作。