在Web API 2项目中,我有一个从数据库中返回项目的控制器,如此;
<Stop>
<Connection>
LineShortName: <LineShortName>10</LineShortName> Destination: <Destination>HAUPTBAHNHOF</Destination> ScheduledDepartureTime: <ScheduledDepartureTime>2017-11-07T10:52:00</ScheduledDepartureTime>
</Connection>
<Connection>
LineShortName: <LineShortName>2/12</LineShortName> Destination: <Destination>WALLERSHEIM</Destination> ScheduledDepartureTime: <ScheduledDepartureTime>2017-11-07T10:47:00</ScheduledDepartureTime>
</Connection>
<Connection>
LineShortName: <LineShortName>1</LineShortName> Destination: <Destination>GOLDGRUBE</Destination> ScheduledDepartureTime: <ScheduledDepartureTime>2017-11-07T11:16:00</ScheduledDepartureTime>
</Connection>
<Connection>
LineShortName: <LineShortName>1</LineShortName> Destination: <Destination>ALTSTADT</Destination> ScheduledDepartureTime: <ScheduledDepartureTime>2017-11-07T10:44:00</ScheduledDepartureTime>
</Connection>
<Connection>
LineShortName: <LineShortName>9</LineShortName> Destination: <Destination>KO-ZENTRUM</Destination> ScheduledDepartureTime: <ScheduledDepartureTime>2017-11-07T11:24:00</ScheduledDepartureTime>
</Connection>
<Connection>
LineShortName: <LineShortName>8</LineShortName> Destination: <Destination>HAUPTBAHNHOF</Destination> ScheduledDepartureTime: <ScheduledDepartureTime>2017-11-07T11:25:00</ScheduledDepartureTime>
</Connection>
<Connection>
LineShortName: <LineShortName>3/13</LineShortName> Destination: <Destination>HAUPTBAHNHOF</Destination> ScheduledDepartureTime: <ScheduledDepartureTime>2017-11-07T11:08:00</ScheduledDepartureTime>
</Connection>
<Connection>
LineShortName: <LineShortName>20</LineShortName> Destination: <Destination>HAUPTBAHNHOF</Destination> ScheduledDepartureTime: <ScheduledDepartureTime>2017-11-07T11:24:00</ScheduledDepartureTime>
</Connection>
<Connection>
LineShortName: <LineShortName>2/12</LineShortName> Destination: <Destination>NEUENDORF</Destination> ScheduledDepartureTime: <ScheduledDepartureTime>2017-11-07T11:32:00</ScheduledDepartureTime>
</Connection>
</Stop>
这可以按预期工作。
但是正如你可能猜到的那样,otherCollection可以包含与当前项目相关的x行。目前我只获得FirstOrDefault。 cX和cY是Lat和Lng坐标。
我现在需要从otherCollection中收集与当前项相关的所有坐标,并将它们作为LatLng的组合列表返回,然后我可以在函数中使用它来查找中心点(此函数I有)。
所以我想做这样的事情:
[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();
}
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
都是字符串值。
我怎么能这样做?
答案 0 :(得分:1)
您将无法使用GetCentralGeoCoordinate
作为IQueryable。如果要调用此函数,首先应将数据提取到本地内存,然后为获取序列的每个元素调用此函数。
var result = db.Items
.Select(item => new ItemsList()
{
Id = item.Id,
...
})
.AsEnumerable() // bring your items to local memory
.Select(item => GetCentralGeoCoordinate(...);
现在每个GeoCoordinate都有两个属性: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
类是一个带有cX和cY的简单类。与System.Drawing.Point
计算中心坐标的函数将采用一系列坐标并返回唯一的中心坐标(如果没有中心坐标,则可能为null)
Coordinate GetCentralGeoCoordinate(IEnumerable<Coordinate> coordinates)
{
foreach (Coordinate coordinate in Coordinates)
{
... // do something with the coordinate
}
}