views:

27

answers:

3

Hi, 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 GeocodeCoord { 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'

Thanks

A: 

Just use ToList(), the compiler will find the correct type for you.

GvS
If I do this _geocode = db.Geocodes.Select(g => new GeocodeCoord { g.postcode, g.x, g.y }).ToList(); I get the same error.If i do the following: _geocode = db.Geocodes.Select(g => new { g.postcode, g.x, g.y }).ToList(); I get Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<Search.GeocodeCoord>'
Mantisimo
A: 

Type in the property names of the initializer like new GeocodeCoord { Postcode = g.postcode, X = g.x, Y = g.y }

Stilgar
Thanks you, yep that works great cheers :)
Mantisimo
A: 

The line should be:

_geocode = db.Geocodes.Select(g => new GeocodeCoord { Outcode = g.postcode, X = g.x, Y = g.y }).ToList();
Lee
Beautiful!! Thanks you very much :).. Just tried it and it works :)
Mantisimo