简体   繁体   English

使用Linq计算平均值

[英]Calculating Average with Linq

I am trying to write a query with Linq to calculate for a genre of a movie for a user (the most watched for that specific user) the average ranking of the movies for that genre (the rankings are in a different table and are given out by many users and I need to calculate the average ranking for each and every movie in that genre). 我正在尝试用Linq编写一个查询来计算用户的电影类型(该特定用户观看次数最多)该类型的电影的平均排名(排名在不同的表中,并给出许多用户,我需要计算该流派中每部电影的平均排名。

my scheme is this: 我的计划是这样的: 在此输入图像描述

my code by now is this: 我现在的代码是这样的:

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
                        };

As you can see, I was able to Isolate the most watched genre for the user(I get the list of movies watched by that user as an input to the query and this input is named results of type IQueryable) I am unable to calculate for each movie in that genre its average score... 正如你所看到的,我能够为用户隔离最受关注的类型(我得到该用户观看的电影列表作为查询的输入,此输入被命名为IQueryable类型的结果)我无法计算该流派中的每部电影的平均分数......

Thank you, 谢谢,

You already have the Ranks for each Movie, so after your code 你已经拥有每部电影的等级,所以在你的代码之后

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
        };

you should get the average, in this case with a lambda expression: 你应该得到平均值,在这种情况下使用lambda表达式:

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

Also, if you just want the average for every user, and don't care about MovieId , you could do everything in your first query: 此外,如果您只想要每个用户的平均值,而不关心MovieId ,您可以在第一个查询中执行所有操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM