I want to get the average rating of each book. The problem is, I don't know how to do that in linq.
Code:
public class Review
{
public int Id { get; set; }
public string Content { get; set; }
public int Rating { get; set; }
public virtual Book Book { get; set; }
}
public class Book
{
public int Id { get; set; }
public string Title { get; set; }
public virtual ICollection<Review> Reviews { get; set; }
}
In sql it would look like this:
select Books.Title, Avg(Reviews.Rating)
from Books
inner join Reviews on Books.Id = Reviews.BookId
group by Books.Title
Output exemple:
Title | Rating
Book1 | 5
Book2 | 2
Book3 | 3
How can I do this query in linq c #?