Cannot initialize type 'class' with a collection initializer because it does not implement 'IEnumerable'
can anyone tell me why this doesnt work?
public class GeocodeCoord
{
public string Outcode { get; set; }
public int X { get; set; }
public int Y { get; set; }
}
List<GeocodeCoord> _geocode;
using (MyDataContext db = new MyDataContext())
{
_geocode = db.Geocodes.Select(g => new Geocod开发者_如何学运维eCoord { g.postcode, g.x, g.y }).ToList<GeocodeCoord>();
}
I get the following error:
Cannot initialize type 'Search.GeocodeCoord' with a collection initializer because it does not implement 'System.Collections.IEnumerable'
The line should be:
_geocode = db.Geocodes.Select(g => new GeocodeCoord { Outcode = g.postcode, X = g.x, Y = g.y }).ToList();
Just use ToList()
, the compiler will find the correct type for you.
Type in the property names of the initializer like new GeocodeCoord { Postcode = g.postcode, X = g.x, Y = g.y }
精彩评论