使用泛型类型&#39; System.Collections.Generic.List <t>&#39;在IRepository中需要1个类型参数

时间:2015-06-24 06:46:56

标签: c# asp.net-mvc generics

我在下面提到的代码中遇到了问题。它给我一个论点。 IRepository

public interface IUserRepository
{
    List GetAll(); // Error shows me here then i change this to List<User> GetAll(); but still getting the error.
}

存储库

public class UserRepository : IUserRepository
{
    private IDbConnection _db = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
    public List<User> GetAll()
    {
        return this._db.Query<User>("SELECT * FROM Users").ToList();
    }
}

1 个答案:

答案 0 :(得分:2)

你应该尝试:

public interface IUserRepository<T>
{
    List<T> GetAll(); 
}

然后:

public class UserRepository : IUserRepository<User>
{
    private IDbConnection _db = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
    public List<User> GetAll()
    {
        return this._db.Query<User>("SELECT * FROM Users").ToList();
    }
}