航班:查找最低价的航班组合 [英] Flights: find lowest price flight combinations

查看:197
本文介绍了航班:查找最低价的航班组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,一个目的地可能有2个联航航班

A destination may have 2 connected flights, for example

从法兰克福到波士顿

航班号为fl1,fl2的法兰克福-伦敦(在08:00-10:00、14:00-18:00等)

Frankfurt-London (at 08:00-10:00, 14:00-18:00 etc.) with flight number fl1, fl2

航班号为lb1,lb2的伦敦-波士顿(在10:00-12:00、16:00-20:00等)

London-Boston (at 10:00-12:00, 16:00-20:00 etc.) with flight number lb1, lb2

每个航班可能都有A,B等舱(从便宜到昂贵)

Each flight may have classes such as A, B etc. (from cheap to expensive)

我已经具有笛卡尔坐标系,例如所有组合:

I have already Cartesian such as of all combinations:

fl1/A-lb1/A(fl1是航班号/A是舱位)
fl1/A-lb1/B
fl1/B-lb1/A
fl1/B-lb1/B
...
fl2/B-lb2/B

fl1/A - lb1/A (fl1 being flight number / A being class)
fl1/A - lb1/B
fl1/B - lb1/A
fl1/B - lb1/B
...
fl2/B - lb2/B

我应该在结束屏幕上显示的是每种目的地组合的最低价格航班:

What should I present at end screen is lowest price flight for each destination combination:

fl1/A-lb1/A
fl1/A-lb2/A
fl2/A-lb1/A
fl2/A-lb2/A

fl1/A - lb1/A
fl1/A - lb2/A
fl2/A - lb1/A
fl2/A - lb2/A

如何通过linq查询/查询实现这一目标?

How can I achieve this with linq query/queries?

我应该怎么找到

我有目的地和航班舱位:

I have Destination and Flight classes:

Class Destination 
{
    List<Flight> Flights
}

Class Flight{
    List<String> @Classes;   //such as A,B,C,D,E  
    String FlightId;       
}

到目前为止,我可以使用以下方法来简化航班列表:

So far I can have flatten list of flights with:

var flights = destination.SelectMany(d=>d.flights);

但是我不知道该如何继续?

But I cannot figure out how to continue?

注意:我希望我在简化实际案例时不会犯错

Note: I hope I did not make mistake while simplifying my real case

这是我在html表结构上的情况:

Here is what it looks like my case on html table structure:

推荐答案

对我来说还不清楚,但是每个目的地是否有两个连接的航班

To me is unclear yet, but if each destination has two connected flights

public class Destination
{
   public Flight Flight1 {get;set;}
   public Flight Flight2 {get;set;}
}

我会做这样的事情:

var comb =   (from dest in destinations
              from fc1 in dest.Fligth1.Classes.Select(s=>new {FlightId=dest.Flight1.FlightId, Class=s})
              from fc2 in dest.Fligth2.Classes.Select(s=>new {FlightId=dest.Flight2.FlightId, Class=s})
              select new {fc1, fc2}).OrderBy(e=>e.fc1.Class).ThenBy(e=>fc2.Class);

如果您想保留Destination中的排期列表,我想您可以使用List的索引器:

If you want to keet your list of Flights in Destination, I guess you can use the indexer of List:

var comb =   (from dest in destinations
              from fc1 in dest.Flights[0].Classes.Select(s=>new {FlightId=dest.Flights[0].FlightId, Class=s})
              from fc2 in dest.Flights[1].Classes.Select(s=>new {FlightId=dest.Flights[1].FlightId, Class=s})
              select new {fc1, fc2}).OrderBy(e=>e.fc1.Class).ThenBy(e=>e.fc2.Class);

这篇关于航班:查找最低价的航班组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆