使用Linq计算平均值

时间:2017-01-06 14:34:56

标签: c# linq average

我正在尝试用Linq编写一个查询来计算一个用户的电影类型(该特定用户观看次数最多)该类型的电影的平均排名(排名在不同的表中,由许多用户发出,我需要计算该类型中每部电影的平均排名。

我的计划是这样的: enter image description here

我的代码现在是:

var extractMoviesAndTheirGenre =
                        from item in results join second in database.MovieGenres on item.movieId equals second.movieId
                        where item.movieId.Equals(second.movieId)
                        select new
                        {
                            MovieId = item.movieId,
                            GenreName = second.Genre.genreName,
                            GenreId = second.genreId
                        };
        var extractGenreNeeded =
                        from item in extractMoviesAndTheirGenre
                        group item by new
                        {
                            GenreName = item.GenreName,
                            GenreId = item.GenreId
                        }
                        into grouped
                        select new
                        {
                            GenreId = grouped.Key.GenreId,
                            GenreName = grouped.Key.GenreName,
                            CountGenre = grouped.Count(x => x.GenreId == grouped.Key.GenreId),
                        };
        extractGenreNeeded = (extractGenreNeeded.OrderByDescending(x => x.CountGenre).Take(1));
    var GenreId = extractGenreNeeded.FirstOrDefault().GenreId.ToString();
        var extractMoviesInFoundGenre =
                        from item in extractGenreNeeded
                        join second in database.MovieGenres on item.GenreId equals second.genreId
                        where second.genreId.Equals(GenreId)
                        select new
                        {
                            MovieId = second.movieId
                        };
        var extractingMoviesInGenreAndTheirAvarageRank =
                        from item in extractMoviesInFoundGenre
                        join second in database.UserMovieRanks on item.MovieId equals second.movieId
                        select new
                        {
                            MovieId = item.MovieId,
                            Rank = second.movieRank
                        };

正如你所看到的,我能够为用户隔离最受关注的类型(我得到该用户观看的电影列表作为查询的输入,此输入被命名为IQueryable类型的结果)我无法计算该流派中的每部电影的平均分数......

谢谢,

1 个答案:

答案 0 :(得分:0)

你已经拥有每部电影的等级,所以在你的代码之后

var extractingMoviesInGenreAndTheirAvarageRank =
        from item in extractMoviesInFoundGenre
        join second in database.UserMovieRanks on item.MovieId equals second.movieId
        select new
        {
            MovieId = item.MovieId,
            Rank = second.movieRank
        };

你应该得到平均值,在这种情况下使用lambda表达式:

double avg = extractingMoviesInGenreAndTheirAvarageRank.Average(x => x.Rank);

此外,如果您只想要每个用户的平均值,而不关心MovieId,那么您可以在第一个查询中执行所有操作:

var extractingMoviesInGenreAndTheirAvarageRank =
        (from item in extractMoviesInFoundGenre
        join second in database.UserMovieRanks on item.MovieId equals second.movieId
        select second.movieRank).Average();