I have the following columns in a table
MovieID - Point - CategoryID
1-5-1
1-6-2
1-4-3
2-7-1 ...
As you can see, there is more than one category for each movie. I want to find a movie with maximum points. But these points should be average in all categories. The following linq query finds me the maximum point as I want, but I also need the movieID.
from tmp in
(from tmp in (
(from Rating in db.Rating
where
Rating.DistrictID == 1
group Rating by new {
Rating.MovieID
} into g
select new {
g.Key.MovieID,
avg_point = (double?)g.Average(p => p.Point)
}))
select new {
tmp.avg_point,
Dummy = "x"
})
group tmp by new { tmp.Dummy } into g
select new {
max_point = (double?)g.Max(p => p.avg_point)
}
------------------------------- Answer Thanks @Sari Rahal. I came up with the following linq query:
(from tmp in (
(from Rating in db.Rating
where
Rating.DistrictID == 1
group Rating by new {
Rating.MovieID
} into g
select new {
g.Key.MovieID,
avg_point = (double?)g.Average(p => p.Point)
}))
orderby
tmp.avg_point descending
select new {
tmp.MovieID,
tmp.avg_point
}).Take(1)
source
share