DbSet 的扩展是什么?我想添加方法“FindTheLatest”。这是我的代码:这是我的代码
公共静态列表 FindTheLatest(此 DbSet<Review> 评论,int nums) { 返回reviews.OrderByDescending(r => r.Created).Take(nums).ToList(); }
但它不起作用。我必须做什么?
这是我的扩展类:
public static class RestaurantReviewsExtenstion
{
public static IEnumerable<Review> FindTheLatest(this IQueryable<Review> reviews, int nums)
{
return reviews.OrderByDescending(r => r.Created).Take(nums).ToList();
}
public static Review FindById(this System.Data.Entity.DbSet<Review> reviews, int id)
{
return reviews.SingleOrDefault(s => s.ReviewId == id);
//return reviews.Find(item => item.Id == id);
}
public static Review FindTheBest(this List<Review> list)
{
return list.OrderByDescending(o => o.Rating).FirstOrDefault();
}
}
这是我的 DBContex 类:
public class OdeToFoodDb: DbContext
{
public DbSet<Restaurant> Restaurants { get; set; }
public DbSet<Review> Reviews { get; set; }
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Restaurant>()
.HasMany(resturant => resturant.Reviews)
.WithRequired(review => review.Resturant);
base.OnModelCreating(modelBuilder);
}
}
这是我得到错误的地方:
OdeToFoodDb _db = new OdeToFoodDb();
public PartialViewResult LatestReview()
{
System.Threading.Thread.Sleep(1500);
//this is where i get error
var review = _db.Reviews.FindTheLatest(1);
//************************************
return PartialView("_Review", review);
}
抱歉信息不全。