将字符串合并为 Queryable 中的 List
Combine strings to List in Queryable
在 Web API 2 项目中,我有一个控制器,可以从数据库中获取 return 项,就像这样;
[EnableQuery()]
public IQueryable<MyList> GetList()
{
var items = from i in db.Items
select new ItemsList()
{
Id = i.Id,
Title = i.Title,
cX = i.otherCollection.FirstOrDefault().cX,
cY = i.otherCollection.FirstOrDefault().cY
};
return items.AsQueryable();
}
这按预期工作。
但正如您可能猜到的那样,otherCollection 可以包含与当前项相关的 x 行。目前我只得到 FirstOrDefault。
cX 和 cY 是 Lat 和 Lng 坐标。
我现在需要从 otherCollection 收集与当前项目相关的所有坐标,并且 return 它们作为 LatLng 的组合列表,然后我可以在函数中使用它来查找中心点(这个功能我有)
所以我想做这样的事情:
var items = from i in db.Items
select new ItemsList()
{
Id = i.Id,
Title = i.Title,
LatLng = GetCentralGeoCoordinate(i.otherCollection.cX.ToList() + i.otherCollection.cY.ToList()) //IList<GeoCoordinate> geoCoordinates
};
return items.AsQueryable();
i.otherCollection.cX
和 i.otherCollection.cY
都是字符串值。
我该怎么做?
您将无法使用 GetCentralGeoCoordinate
作为 IQueryable。如果你想调用这个函数,你应该首先将数据获取到本地内存,然后为你获取的序列的每个元素调用这个函数。
var result = db.Items
.Select(item => new ItemsList()
{
Id = item.Id,
...
})
.AsEnumerable() // bring your items to local memory
.Select(item => GetCentralGeoCoordinate(...);
现在每个地理坐标都有两个属性:cX 和 cY。找中心坐标的时候需要知道哪个cx属于哪个cy。你想要这样的东西:
var coordinates = (4, 10) (8, 15) (3, 7) ...
而不是
var xCoordintate = 4, 8, 3, ...
var yCoordinates = 10, 15, 7, ...
您的查询是:
var items = db.Items
select (item => new ItemsList()
{
Id = item.Id,
Title = item.Title,
Coordinates = item.OtherCollection
.Select(otherCollectionItem => new Coordinate()
{
Cx = otherCollectionItem.Cx,
Cy = otherCollectionItem.Cy,
})
.ToList(),
}
.AsEnumerable()
.Select(fetchedItem => GetCentralGeoCoordinate(fetchedItem.Coordinate));
Coordinate
class 是一个带有 cX 和 cY 的简单 class。类似于 System.Drawing.Point
您计算中心坐标的函数将采用一系列坐标和 returns 唯一的中心坐标(如果没有中心坐标,则可能为 null)
Coordinate GetCentralGeoCoordinate(IEnumerable<Coordinate> coordinates)
{
foreach (Coordinate coordinate in Coordinates)
{
... // do something with the coordinate
}
}
在 Web API 2 项目中,我有一个控制器,可以从数据库中获取 return 项,就像这样;
[EnableQuery()]
public IQueryable<MyList> GetList()
{
var items = from i in db.Items
select new ItemsList()
{
Id = i.Id,
Title = i.Title,
cX = i.otherCollection.FirstOrDefault().cX,
cY = i.otherCollection.FirstOrDefault().cY
};
return items.AsQueryable();
}
这按预期工作。
但正如您可能猜到的那样,otherCollection 可以包含与当前项相关的 x 行。目前我只得到 FirstOrDefault。 cX 和 cY 是 Lat 和 Lng 坐标。
我现在需要从 otherCollection 收集与当前项目相关的所有坐标,并且 return 它们作为 LatLng 的组合列表,然后我可以在函数中使用它来查找中心点(这个功能我有)
所以我想做这样的事情:
var items = from i in db.Items
select new ItemsList()
{
Id = i.Id,
Title = i.Title,
LatLng = GetCentralGeoCoordinate(i.otherCollection.cX.ToList() + i.otherCollection.cY.ToList()) //IList<GeoCoordinate> geoCoordinates
};
return items.AsQueryable();
i.otherCollection.cX
和 i.otherCollection.cY
都是字符串值。
我该怎么做?
您将无法使用 GetCentralGeoCoordinate
作为 IQueryable。如果你想调用这个函数,你应该首先将数据获取到本地内存,然后为你获取的序列的每个元素调用这个函数。
var result = db.Items
.Select(item => new ItemsList()
{
Id = item.Id,
...
})
.AsEnumerable() // bring your items to local memory
.Select(item => GetCentralGeoCoordinate(...);
现在每个地理坐标都有两个属性:cX 和 cY。找中心坐标的时候需要知道哪个cx属于哪个cy。你想要这样的东西:
var coordinates = (4, 10) (8, 15) (3, 7) ...
而不是
var xCoordintate = 4, 8, 3, ...
var yCoordinates = 10, 15, 7, ...
您的查询是:
var items = db.Items
select (item => new ItemsList()
{
Id = item.Id,
Title = item.Title,
Coordinates = item.OtherCollection
.Select(otherCollectionItem => new Coordinate()
{
Cx = otherCollectionItem.Cx,
Cy = otherCollectionItem.Cy,
})
.ToList(),
}
.AsEnumerable()
.Select(fetchedItem => GetCentralGeoCoordinate(fetchedItem.Coordinate));
Coordinate
class 是一个带有 cX 和 cY 的简单 class。类似于 System.Drawing.Point
您计算中心坐标的函数将采用一系列坐标和 returns 唯一的中心坐标(如果没有中心坐标,则可能为 null)
Coordinate GetCentralGeoCoordinate(IEnumerable<Coordinate> coordinates)
{
foreach (Coordinate coordinate in Coordinates)
{
... // do something with the coordinate
}
}