[英]LINQ Group results and select from another table
I have the following database structure: 我具有以下数据库结构:
table RoadSegment
int SegmentID
double StartKm
double EndKm
table GeographicData
double Km
double latitude
double longitude
table RoadFeature
int FeatureID
int SegmentID
And I want to select a list of all road features within a certain kilometer range, but i'd also like to get all of the geographic coordinates in each feature's segment. 我想选择某个公里范围内所有道路要素的列表,但我也想获取每个要素段中的所有地理坐标。 Is there anyway I can do this in just one database trip (a single LINQ statement)?
无论如何,我只需一次数据库旅行(一个LINQ语句)就可以做到这一点? I've tried the following:
我尝试了以下方法:
from feat in RoadFeature
from geo in GeographicData
join seg in RoadSegment on feat.SegmentID equals seg.SegmentID
where geo.Km >= seg.StartKm && geo.Km <= seg.EndKm
group feat by feat.FeatureID into grp
select new {
Feature = grp.Key,
GeoData = grp.ToList()
}
But all I get is, for each feature, a list of features with the same ID. 但是,对于每个功能,我得到的只是具有相同ID的功能列表。 How can i proceed to get, for each feature, the list of coordinates on its segment ?
如何为每个要素继续获取其线段上的坐标列表 ? That is, I'd like to have the following result out of the query:
也就是说,我想从查询中得到以下结果:
[
{
FeatureID: 1,
GeoData: [
[-11.786783, 22.4567864],
[-11.654684, 22.6546548],
[-11.646648, 22.7867544]
]
},
{
FeatureID: 2,
GeoData: [
[-11.111265, 22.4118787],
[-11.568746, 22.6546548],
[-11.124687, 22.0121571]
]
}
]
Some data: 一些数据:
RoadSegment
ID StartKm EndKm
41 57.6 69.4
42 69.4 80
43 80 86.5
44 86.5 92.2
45 92.2 126
46 126 132
RoadFeature
FeatureID SegmentID
1 41
2 42
3 43
4 44
5 45
6 46
GeographicData
Km latitude longitude
57.62 -19.959059 -44.339733
57.622 -19.959121 -44.339793
57.631 -19.959182 -44.339857
57.641 -19.959249 -44.339922
57.651 -19.959317 -44.339989
57.661 -19.959387 -44.340062
57.672 -19.959465 -44.340145
57.684 -19.959548 -44.340232
57.697 -19.959632 -44.340322
57.71 -19.959721 -44.340419
57.724 -19.959815 -44.340522
57.739 -19.959913 -44.34063
57.755 -19.960013 -44.340739
57.771 -19.960114 -44.340846
57.787 -19.960213 -44.34095
(Point is, there is 1 feature per segment, but a WHOLE LOT of geo-data per kilometer. Also, i'd like solutions which don't rely on the uniqueness of feature per segment, if possible) (要点是,每个路段只有1个要素,但是每公里有大量的地理数据。此外,如果可能的话,我希望解决方案不依赖每个路段的唯一性)
Think you just need to add geo
in your grouping. 认为您只需要在分组中添加
geo
。
Then you can select these datas in your anonymous type. 然后,您可以以匿名类型选择这些数据。
So 所以
from feat in RoadFeature
from geo in GeographicData
join seg in RoadSegment on feat.SegmentID equals seg.SegmentID
where geo.Km >= seg.StartKm && geo.Km <= seg.EndKm
group new {feat, geo} by feat.FeatureID into grp
select new {
Feature = grp.Key,
GeoData = grp.Select(x => x.geo).ToList()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.