简体   繁体   中英

Cannot initialize type x with a collection initializer because it does not implement 'System.Collections.IEnumerable'

I am proving this code but it sends me the error about the collection initialize.

Cannot initialize type x with a collection initializer because it does not implement 'System.Collections.IEnumerable'

I am trying this with 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" }

    };

My class List

class List
{

}

I think you need to use List<T> from System.Collections.Generic instead of creating your own List class. Your list class is just an empty class. In order to achieve what you are doing, remove your list class, import the namespace System.Collections.Generic and use List<T> .

Here is your code using List

Remove your List class.

And then import System.Collections.Generic namespace like this.

Using System.Collections.Generic;

And then use following code.

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" }

};

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