[英]LINQ select from join table where the same foreign key has records for two different IDs
Ok, so the title is a little bit confusing I guess. 好的,所以标题有点混乱我猜。 Basically I have those 3 tables: 基本上我有这3个表:
Line
id | Name
---------
1 | "A-B"
2 | "A-D"
Stop
id | Name
---------
1 | A
2 | B
3 | C
4 | D
LineStop
Id | LineId | StopId | Order
----------------------------
1 | 1 | 1 | 0
2 | 1 | 2 | 1
3 | 2 | 1 | 0
4 | 2 | 2 | 1
5 | 2 | 3 | 3
4 | 2 | 4 | 4
So this is some sort of bus ticketing system which I work on of personal improvement. 所以这是某种公共汽车票务系统,我从事个人改进工作。
As an input I get the departure StopId (Stop.Id) and the arrival StopId (Stop.Id). 作为输入,我得到离开StopId(Stop.Id)和到达StopId(Stop.Id)。 I want to select all lines that has those two stops in their routes (This would mean that in LineSop
table for the same LineId
I'll have records with both the departuring and arrival stops, ultimately I would also like to consider the Order
column which tells in what order the bus is going through those Stops, because even if the line have the two stops I'm interested in, if they are in reversed order I'm still not interested. 我想选择在他们的路线中有两个停靠点的所有行(这意味着在同一LineId
LineSop
表中我将有离开和到达停止的记录,最终我还想考虑Order
列告诉公交车通过那些停靠点的顺序,因为即使线路有两个我感兴趣的站点,如果它们按相反顺序排列,我仍然不感兴趣。
I know that is highly desirable to show what I've done so far but I struggle with the where conditions which seems to be the key factor here. 我知道这是非常可取的,以显示我到目前为止所做的事情,但我在这里似乎是关键因素的条件。 For some reason I decided to join Line
with LineStop
: 出于某种原因,我决定加入Line
与LineStop
:
var lines = _context.Lines.Join(
_context.LineStop,
line => line.Id,
lineStop => lineStop.LineId,
(line, lineStop) => lineStop)
But then.. I need to check if for the same LineId
I have records in LineStop
table with the start and end StopId
and ultimately when I found such records the the starting StopId
Order i less than the end StopId
Order. 但那时..我需要检查是否对于相同的LineId
我在LineStop
表中有开始和结束StopId
记录,并且最终当我找到这样的记录时,起始StopId
Order i小于结束StopId
Order。
I hope this can help you out: 我希望这可以帮助你:
First I get the trip from the traveler: "I want go from Stop: 2 to Stop:4". 首先,我从旅行者那里得到了旅行:“我想从Stop:2到Stop:4”。 Once I know the line that has both stops I build the stops and its order. 一旦我知道有两个停靠点的线,我就建立止损及其顺序。
var lines = new List<Line>()
{
new Line() { Id = 1, Name = "A-B" },
new Line() { Id = 2, Name = "A-D" }
};
var stops = new List<Stop>() {
new Stop() { Id = 1, Name = "A" },
new Stop() { Id = 2, Name = "B" },
new Stop() { Id = 3, Name = "C" },
new Stop() { Id = 4, Name = "D" }
};
var lineStops = new List<LineStop>()
{
new LineStop() { Id = 1, LineId = 1, StopId = 1, Order = 0 },
new LineStop() { Id = 2, LineId = 1, StopId = 2, Order = 1 },
new LineStop() { Id = 3, LineId = 2, StopId = 1, Order = 0 },
new LineStop() { Id = 4, LineId = 2, StopId = 2, Order = 1 },
new LineStop() { Id = 5, LineId = 2, StopId = 3, Order = 3 },
new LineStop() { Id = 4, LineId = 2, StopId = 4, Order = 4 },
};
var result = (from trip in (from l in lines
join d in lineStops on l.Id equals d.LineId
join a in lineStops on l.Id equals a.LineId
where d.StopId == 2 && a.StopId == 4
select new { d.LineId })
join l in lines on trip.LineId equals l.Id
join ls in lineStops on l.Id equals ls.LineId
select new { l.Name, ls.StopId, ls.Order }).OrderBy(x => x.Order);
Expected result 预期结果
Name StopId Order
A-D 1 0
A-D 2 1
A-D 3 3
A-D 4 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.