在Linq查询中创建列表
本文关键字:创建 列表 查询 Linq | 更新日期: 2023-09-27 18:03:48
我试图在Linq
中的select new
中创建List
以设置对象的属性。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)
我怎么能把它变成coordinates
属性的List<LatLng>
,这是一个List<LatLng>
类型?
试试这样
var query = from loc in locList select
new SomeObject {
coordinates = new List<LatLng> {
new LatLng(loc.Lat,loc.Lon)
}
}