简体   繁体   中英

extension for DbSet

What is extension for DbSet? I want to add method 'FindTheLatest'. this is my code: this is my code

public static List FindTheLatest(this DbSet<Review> reviews, int nums)
    {
        return reviews.OrderByDescending(r => r.Created).Take(nums).ToList();
    }

But it doesn't work. What i must to do?

this is my extension class:

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

this is my DBContex Class:

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

this is where i get error:

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

Sorry about poor information.

Well for one thing, List is a generic type. For another DbSet isn't a set of any specific type - you probably want DbSet<T> for some appropriate T . Perhaps you mean:

public static List<Review> FindTheLatest(this DbSet<Review> reviews, int nums)
{
    return reviews.OrderByDescending(r => r.Created).Take(nums).ToList();
}

Or:

public static List<Review> FindTheLatest(this DbSet reviews, int nums)
{
    return reviews.Cast<Review>()
                  .OrderByDescending(r => r.Created)
                  .Take(nums)
                  .ToList();
}

you can implement extension method using Generic to accept any type:

public static class DbSetExtensions
{
    public static IQueryable<T> FindTheLast<T,TResult>(this IQueryable<T> t, Expression<Func<T, TResult>> expression, int nums) where T : class 
    {
      return t.OrderByDescending(expression).Take(nums);
    }
}  

and use it :

dbContext.Entity.FindTheLast(x => x.Property,2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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