This project has moved and is read-only. For the latest updates, please go here.
1
Vote

LatLng needs a default constructor to work properly with LINQ

description

I've been trying to run the following LINQ query to convert a collection of geocoded addresses into Pushpins:
 
var contacts = _contactRepository
.All
.Where(contact =>
    contact.HomeAddress.Longitude >= minLng &&
    contact.HomeAddress.Longitude <= maxLng &&
    contact.HomeAddress.Latitude >= minLat &&
    contact.HomeAddress.Latitude <= maxLat)
.Select(contact => new Pushpin(contact.HomeAddress.Latitude, contact.HomeAddress.Longitude));
 
return new MapDataResult { Pushpins = contacts };
 
but the query fails because the .Select part of the query will only work when the objects being constructed have a default constructor, and LatLng lacks a default constructor, so the following code will also fail:
 
var contacts = _contactRepository
.All
.Where(contact =>
    contact.HomeAddress.Longitude >= minLng &&
    contact.HomeAddress.Longitude <= maxLng &&
    contact.HomeAddress.Latitude >= minLat &&
    contact.HomeAddress.Latitude <= maxLat)
.Select(contact => new Pushpin{ Position = new LatLng(contact.HomeAddress.Latitude, contact.HomeAddress.Longitude));
 
return new MapDataResult { Pushpins = contacts };
 
The workaround is to split up the single LINQ query, and have the creation of the Pushpin collection inside a regular foreach loop, where we can use the constructor overloads as we like; but it would still nice to be able to do it in a single LINQ query; hence, please could you add a default constructor to LatLng?!

comments