首先感谢耐心阅读这么久的问题。这是迄今为止我可以制作的最短版本。
这个问题是my previous question的子问题。一旦我澄清它,我会更新它并要求重新打开然后我可以回答它
我的问题是nth
以下代码组合的实用方法。
我有行程编组的连接航班,其数量为1,2 .. n 每个航班可能有A,B,C等级......
我将每个航班与同一班级相结合
这是我的代码产生组合:
//combination of ONE flightSegments
var firstFlightSegments = flightSegments.Where(s => s.Count == 1).ToList();
var originDestination =
(from firstFlightSegment in firstFlightSegments
select
new originDestination {
pnr = Utils.GeneratePnr(),
flightSegments = new List<FlightSegment>{firstFlightSegment}
}
)
//combinations of TWO flightSegments
var firstFlightSegments = flightSegments.Where(s => s.Count == 1).ToList();
var secondSecondSegments = flightSegments.Where(s => s.Count == 2).ToList();
var originDestination =
(from firstFlightSegment in firstFlightSegments
join secondFlightSegment in secondSecondSegments
on firstFlightSegments.flightClass equals secondSegment.flightClass
select
new originDestination {
pnr = Utils.GeneratePnr(),
flightSegments = new List<FlightSegment>{firstFlightSegment, secondFlightSegment}
}
)
//combinations of THREE flightSegments
var firstFlightSegments = flightSegments.Where(s => s.Count == 1).ToList();
var secondSecondSegments = flightSegments.Where(s => s.Count == 2).ToList();
var thirdFlightSegments = flightSegments.Where(s => s.Count == 3).ToList();
var originDestination =
(from firstFlightSegment in firstFlightSegments
join secondFlightSegment in secondSecondSegments
on firstFlightSegments.flightClass equals secondSegment.flightClass
join thirdFlightSegment in thirdFlightSegments
on secondSegment.flightClass equals thirdFlightSegment.flightClass
select
new originDestination {
pnr = Utils.GeneratePnr(),
flightSegments = new List<FlightSegment>{firstFlightSegment, secondFlightSegment, thirdFlightSegment}
}
//HOW TO DO combinations of N flightSegments
???
修改
在@Rhumborl回答哪个方面根据上方工作正常之后,我需要添加此代码以反映我需要的内容MyIndex
我将在其上进行分组
var originDestination =
(from firstFlightSegment in firstFlightSegments
join secondFlightSegment in secondSecondSegments
on firstFlightSegments.flightClass equals secondSegment.flightClass
select
new originDestination {
pnr = Utils.GeneratePnr(),
myIndex = firstFlightSegment.FligthNumber + secondFlightSegment.FligthNumber
flightSegments = new List<FlightSegment>{firstFlightSegment, secondFlightSegment}
}
)
答案 0 :(得分:1)
您希望获得航班的所有航段,并按照其计数顺序将其作为列表返回?
一个简单的GroupBy
会这样做:
var flights = fightSegments
.GroupBy(fs => fs.flightClass) // get all steps for each flight together
.Select(fc =>
new originDestination {
//flightClass = fc.Key, // if you need this
pnr = Utils.GeneratePnr(),
flightSegments = fc // fc is the list of steps
.OrderBy(s => s.Count) // make sure it is in order
.ToList()
}
)
如果您只想要至少有n个步骤的航班,那么您需要做更多的工作来添加额外的where子句:
var flights = fightSegments
.GroupBy(fs => fs.flightClass)
.Where(fcg => fcg.Count() >= n) // make sure flight has n+ steps
.Select(fc =>
new originDestination {
//flightClass = fc.Key, // if you need this
pnr = Utils.GeneratePnr(),
flightSegments = fc
.OrderBy(s => s.Count)
.ToList()
}
)