Question

I am having trouble getting this code to compile. I am new to MVC/Stackoverflow/programming and trying to follow a tutorial. Also, I tried looking some things up and saw some options with Nullable values, but wasn't having much luck.

public class ReviewsController : Controller
{
    OdeToFoodDB _db = new OdeToFoodDB();
    //
    // GET: /Reviews/

    //default action
    public ActionResult Index()
    {
        var model = _db.Reviews.FindTheLatest(3);
        return View(model);
    }

Here is the definition for the FindTheLatest

public static IEnumerable<RestaurantReview> FindTheLatest(this IList<RestaurantReview>        
reviews, int numberOfReviews)
    {
        return reviews.OrderByDescending(r => r.Created)
                      .Take(numberOfReviews);
   } 

Here is also the definition to the OdeToFood if that is needed.

namespace OdeToFood.Models
{
public class OdeToFoodDB : DbContext
{
    public DbSet<Restaurant> Restaurants { get; set; }
    public DbSet<RestaurantReview> Reviews { get; set; }
}
}

I am getting an error saying: 'System.Data.Entity.DbSet' does not contain a definition for 'FindTheLatest' and the best extension method overload 'OdeToFood.Queries.RestaurantReviewQueries.FindTheLatest(System.Collections.Generic.IList, int)' has some invalid arguments

Was it helpful?

Solution

Change your extension method to use IEnumerable<T>, a common base type shared by both DbSet<T> and List<T>:

public static IEnumerable<RestaurantReview> FindTheLatest(
           this IEnumerable<RestaurantReview> reviews, int numberOfReviews)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top