Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to do multiple joins when using groupby in linq
    primarykey
    data
    text
    <p>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.</p> <p>This is my picturerating table :</p> <pre><code>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; } } </code></pre> <p>This is my code to do this :</p> <pre><code> public IQueryable&lt;SortedDto&gt; GetRatignsAndTheirDetails() { var pastDate = DateTime.Now.Date.AddDays(-7); var combo = from p in db.picturerating where p.iddatetime &gt; 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; } </code></pre> <p>Now here comes my problem:</p> <p>How to join two more tables , users and picturedetails table to show username and total rating.</p> <p>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:</p> <pre><code> 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 &gt; 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 =&gt; (float?)l.rating)?? 0 , // pl.Where(a =&gt; a.ratenumber!= null).Average(c =&gt; c.ratenumber) }; return combo; </code></pre> <p>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. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload