无法使用集合初始值设定项初始化类型x,因为它没有实现' System.Collections.IEnumerable'

时间:2015-04-20 04:26:45

标签: c# asp.net-mvc asp.net-mvc-5

我正在证明这段代码,但它向我发送了有关集合初始化的错误。

  

无法使用集合初始值设定项初始化类型x,因为它未实现' System.Collections.IEnumerable'

我正在尝试使用MVC 5

private readonly List clients = new List()
   {
        new Pruebas.Models.Client { Id = 1, Name = "Julio Avellaneda", Email = "julito_gtu@hotmail.com" },
        new Pruebas.Models.Client { Id = 2, Name = "Juan Torres", Email = "jtorres@hotmail.com" },
        new Pruebas.Models.Client { Id = 3, Name = "Oscar Camacho", Email = "oscar@hotmail.com" },
        new Pruebas.Models.Client { Id = 4, Name = "Gina Urrego", Email = "ginna@hotmail.com" },
        new Pruebas.Models.Client { Id = 5, Name = "Nathalia Ramirez", Email = "natha@hotmail.com" },
        new Pruebas.Models.Client { Id = 6, Name = "Raul Rodriguez", Email = "rodriguez.raul@hotmail.com" },
        new Pruebas.Models.Client { Id = 7, Name = "Johana Espitia", Email = "johana_espitia@hotmail.com" }

    };

我的班级列表

class List
{

}

1 个答案:

答案 0 :(得分:1)

我认为您需要使用List<T>中的System.Collections.Generic而不是创建自己的List类。您的列表类只是一个空类。为了实现您的目标,请删除列表类,导入名称空间System.Collections.Generic并使用List<T>

以下是使用List

的代码

删除List类。

然后像这样导入System.Collections.Generic命名空间。

Using System.Collections.Generic;

然后使用以下代码。

private readonly List<Pruebas.Models.Client> clients = new List<Pruebas.Models.Client>()
{
        new Pruebas.Models.Client { Id = 1, Name = "Julio Avellaneda", Email = "julito_gtu@hotmail.com" },
        new Pruebas.Models.Client { Id = 2, Name = "Juan Torres", Email = "jtorres@hotmail.com" },
        new Pruebas.Models.Client { Id = 3, Name = "Oscar Camacho", Email = "oscar@hotmail.com" },
        new Pruebas.Models.Client { Id = 4, Name = "Gina Urrego", Email = "ginna@hotmail.com" },
        new Pruebas.Models.Client { Id = 5, Name = "Nathalia Ramirez", Email = "natha@hotmail.com" },
        new Pruebas.Models.Client { Id = 6, Name = "Raul Rodriguez", Email = "rodriguez.raul@hotmail.com" },
        new Pruebas.Models.Client { Id = 7, Name = "Johana Espitia", Email = "johana_espitia@hotmail.com" }

};