简体   繁体   English

DbSet的扩展

[英]extension for DbSet

What is extension for DbSet? DbSet的扩展名是什么? I want to add method 'FindTheLatest'. 我想添加方法“ 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: 这是我的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);
    }
}

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. 一方面, List是一种通用类型。 For another DbSet isn't a set of any specific type - you probably want DbSet<T> for some appropriate T . 对于另一个DbSet来说,它不是任何特定类型的集合-您可能需要DbSet<T>作为一些适当的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);

暂无
暂无

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

相关问题 Linq:IQueryable扩展方法适用于DBSet但不适用于ICollection - Linq: IQueryable extension methods works on DBSet but not on ICollection 为 DbSet 创建扩展方法是一种不好的做法吗<t> ?</t> - Is it a bad practice to create extension methods for DbSet<T>? 无法使用DbSet的扩展名更新实体 - Cannot update entity using extension of DbSet 数据库集<tablename>不包含“Where”的定义和最佳扩展方法重载</tablename> - DbSet<TableName> does not contain a definition for 'Where' and the best extension method overload 在 DbSet 上使用 LINQ 扩展方法时出现歧义调用<T> - Ambiguous call when using LINQ extension method on DbSet<T> DbSet <xxx> &#39;不包含&#39;Update&#39;的定义,也没有可访问的扩展方法&#39;Update&#39; - DbSet<xxx>' does not contain a definition for 'Update' and no accessible extension method 'Update' 错误 CS1061:“DbSet<t> '不包含'FromSql'的定义并且没有扩展方法'FromSql'接受类型为'DbSet的第一个参数<t> '</t></t> - Error CS1061: 'DbSet<T>' does not contain a definition for 'FromSql' and no extension method 'FromSql' accepting a first argument of type 'DbSet<T>' 实体框架单元测试(或类似测试),以确保在使用DbSet时始终调用特定的扩展方法 - Entity Framework unit test (or similar) to ensure that when using a DbSet, an particular extension method is always called 带有 DbSet 的 JsonResults - JsonResults with DbSet 过滤 DBSet、DBSet.AsQueryable() 或 DBSet.AsQueryable<t> ()</t> - Filter on DBSet, DBSet.AsQueryable() or DBSet.AsQueryable<T>()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM