2

The target is to get list of 10 popular movies.

There is a table called populars in the database that contains: moviecount - the number of times the film was rented. MovieID - id of the film.

There's also a table called Movies that contains all the data of the movies. This table contains a field for MovieID. The tables are not related and should not be linked between them.

I took the 10 id's of the most popular films

var TopTen = videoLibDB.populars
                 .Take(10)
                 .OrderBy(e => e.movieCount)
                 .Select(e => new { e.MovieID });

This is ok, but how do I create a List of the 10 best movies from "movies" table by using 10 MovieIDs of the populars table(the TopTen of the code above)?

In case of one id I can compare the moiveid from the popular table to the movieid in the movies table.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Amir Cabili
  • 21
  • 1
  • 3

5 Answers5

2

First, make sure to sort (OrderBy) before take. In LINQ to SQL/EF, this may not be significant, but with LINQ to Objects it will result in only the first ten rows being evaluated.

Second, you need to either join your populars with the movies, or use the association between the tables. Assuming you have an association set-up, you should be able to do something like the following:

var TopTen = videoLibDB.populars
             .OrderByDescending(e => e.movieCount)
             .Take(10)
             .Select(e => new { e.MovieID, e.Movie.MovieName});
Jim Wooley
  • 10,169
  • 1
  • 25
  • 43
  • Should probably be OrderByDescending. – Joachim Isaksson Sep 18 '12 at 18:47
  • The tables are not related and cannnot be linked between them – Amir Cabili Sep 18 '12 at 18:47
  • @JoachimIsaksson Thanks for the catch. Corrected in the answer. – Jim Wooley Sep 18 '12 at 18:58
  • @AmirCabili Are the tables in the same database? If so, you can use assocations in your model even if they aren't in the database with either LINQ to SQL or EF. If you don't want to use associations, you can use a join. If they are not in the same database, you will need to fetch the values and then join them via LINQ to Objects in the client or use Contains if you know the in memory collection contains less than 2000 records. Please clarify where the requirement "cannot be linked between them" came from. – Jim Wooley Sep 18 '12 at 19:01
2

If you have the relations set up correctly, @JimWooley's answer above is the easiest/best, this query is in case you don't have that possibility.

You have some problems in your linq query, you're taking 10 random movies and ranking only them, least popular first. This query fixes that and joins it with the movie table to get the most popular movies;

var query = 
    from movie in videoLibDB.movies
    where
        videoLibDB.populars
        .OrderByDescending(x => x.movieCount)   // Sort all, most popular first
        .Take(10)                               // but only keep the 10 first
        .Select(x => x.MovieID)                 // Take their MovieID 
        .Contains(movie.MovieID)                // and get movie data on them
    select movie;
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
0

Try this, it should generate an IN clause.

var results = from q in videoLibDB.movies
              where videoLibDB.populars
                    .OrderBy(e => e.movieCount)
                    .Take(10)
                    .Select(e => e.MovieID)
                    .ToArray()
                    .Contains(q.MovieID)
              select q

And here is an article that goes more in depth.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
0

Use .Contains

First create an array containing the IDs....

var ids = new[] { 1,2,3,4,5,6,7,8,9,10 };

Then use .Contains

var TopTen = videoLibDB.populars
    .Where(e => ids.Contains(e.MovieID)
    .OrderBy(e => e.movieCount)
    .Select(e => new { e.MovieID });

It's the equivalent of an IN statement in T-SQL

Antony Scott
  • 21,690
  • 12
  • 62
  • 94
0

i don't see why you have that restriction that the tables may not be linked ... but hey...

var TopTen = videoLibDB.populars
       .OrderBy(e => e.movieCount)
       .Take(10)
       .Select(e => e.MovieID).ToArray();

var YourActualMovies = videoLibDB.movies
       .Where(x=>TopTen.Conatins(x.MovieID))
       .Select(...whatever you want...)
DarkSquirrel42
  • 10,167
  • 3
  • 20
  • 31