开发者

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 }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜