I have a picturerating table in my database. For every new rating ,a new picture rating table row will be created. I am grouping those ratings by lastweek and pictureid, meaning all the ratings for one single pic in the last week will be accumulated and become one rating.Then i will be able to sort them by thier count.
This is my picturerating table :
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; }
}
This is my code to do this :
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;
}
Now here comes my problem:
How to join two more tables , users and picturedetails table to show username and total rating.
As per now i am able to write this code, this is just a rough idea in my mind not a proper code so i need help to change this code to make it work:
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;
Am i doing it right, whats the way to do it, how to change the above code to make it work properly as per my needs.