Heinrich
Heinrich

Reputation: 1761

Create List in Linq query

I am trying to create a List in a select new in Linq to set the property of an object. The linq query is below the object definitions.

public class SomeObject
{
    public List<LatLng> coordinates{get;set;}
}

public class LatLng
{
    public double Lat;
    public double Lon;

    public LatLng(double lat, double lon)
    {
      this.Lat = lat;
      this.Lon = lon;
    }
}


List<LatLng> coordinates = null;
var query = from loc in locList
            select new SomeObject
             (
                coordinates = new LatLng(loc.Lat,loc.Lon)
                // I tried the line below but it doesn't work.
                //coordinates =  new LatLng(loc.Lat,loc.Lon).ToList()                    
             );

The important line is this

coordinates = new LatLng(loc.Lat,loc.Lon)

How can I make that into a List<LatLng> for the coordinates property which is a List<LatLng> type?

Upvotes: 2

Views: 1762

Answers (1)

Krumelur
Krumelur

Reputation: 32597

Try something like this

var query = from loc in locList select 
  new SomeObject { 
    coordinates = new List<LatLng> { 
      new LatLng(loc.Lat,loc.Lon) 
    }
  }

Upvotes: 1

Related Questions