I've been struggling with this so long I really need to know whether you guys can help me.
I tried Google, I tried your forum, the documentation, Linq in Action, LINQ Pocket reference and so on but really don't have an answer.
Data model:
--------- 1 * ------------------- * 1 ------------
| Video | ------->| VideoRating |<-----------| User |
--------- ------------------- -------------
VideoRating has a field called Rating which is a number showing the rating the user gave the specific video.
Video has a field called OverallRating which is the average of all the ratings (from VideoRating table) for this specific Video.
I want to retrieve a specific Video and that is all fine, no problems.
However, for the fetched VideoEntity I also want it's field OverallRating to be fetched as the average of all the Ratings from table VideoRating.
I am looking for something similar to (the code below is not working, see it as pseudo code):
var videos = from video in linqMetaData.Video
let video.OverallRating = (from rating in linqMetaData.VideoRating
where rating.VideoId == video.VideoId
select rating.Rating).Average()
select (video);
I use Linq but would be happy with a non-Linq solution as well.
Worth noting, I don't want the result as a new anonymous type with two properties (1: the VideoEntity object, 2: the OverallRating). I want the VideoEntity object to have its own field OverallRating set to the average.
Very grateful if you could at least point me in some direction. The solution is probably much simpler than I think.