我正在尝试创建一个 List
在select new
在Linq
设置对象的属性。 linq 查询在对象定义下方。
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()
);
重要的是这一行
coordinates = new LatLng(loc.Lat,loc.Lon)
我怎样才能把它变成 List<LatLng>
对于 coordinates
List<LatLng>
属性类型?
最佳答案
尝试这样的事情
var query = from loc in locList select
new SomeObject {
coordinates = new List<LatLng> {
new LatLng(loc.Lat,loc.Lon)
}
}
关于c# - 在 Linq 查询中创建列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31549891/