简体   繁体   English

LINQ从连接表中选择,其中相同的外键具有两个不同ID的记录

[英]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 : 出于某种原因,我决定加入LineLineStop

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.

相关问题 LINQ查询加入两个表并从表A对应的表B中选择最近的记录 - LINQ Query To Join Two Tables and Select Most Recent Records from Table B corresponding to Table A LINQ to SQL连接两个表以基于子表中的两个不同列两次选择父表 - LINQ to SQL join two tables to select parent table twice based on two different columns from child table 用两个外键联接两个表 - join two table with two foreign Key LINQ 查询连接以获取右表没有匹配记录的左表条目 - LINQ query join to get entries for left table where right table has no matching records 一次将两个记录插入两个表,其中一个表将另一个用作外键? - Insert two records at once into two tables where one table uses the other as foreign key? Linq使用4个不同的外键连接同一张表 - Linq joining same table with 4 different foreign keys Linq 外键选择 - Linq Foreign Key Select 如何使用linq从两个表中选择列 - how to select columns from two table with where using linq 如何在不同的表中映射两个具有相同主键的不同外键? - How can I map two different foreign key with same primary key in different table? LINQ to SQL并为单个主键向外部表添加多个记录 - LINQ to SQL and adding multiple records to foreign table for a single primary key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM