My Flight class looks like:
public class Flight{
public string From {get;set;}
public string To {get;set;}
}
I have flatten list of Flight which are composed n Flight (Note: Flight n may have n From - To)
var listOfFromTo = new List<Flight>{
new Flight{ From = "A", To="B"},
new Flight{ From="B", To="C"},
new Flight{ From="D", To="E"},
new Flight{ From="E", To="F"},
new Flight{ From="F", To="G"}
//new Flight {From="...", To="..."}
};
What I want to achive is:
var listOfListOfFlights = new List<List<Flight>>(){flight1, flight2};
Where flight1 and flight2 are
var flight1 = new List<Flight>{
new Flight{ From="A", To="B"},
new Flight{ From="B", To="C"}
};
var flight2 = new List<Flight>{
new Flight{ From="D", To="E"},
new Flight{ From="E", To="F"},
new Flight{ From="F", To="G"}
};
I can not figure out about how to do it but I can guess I should use group by somehow
var result1 =
from loft in listOfFromTo
group loft by new { loft.From, loft.To }
into r
select new Flight {
From = r.Key.From,
To = r.Key.To
};
EDIT I would like to share @bit's algorithm's result in linqpad:
>` would be too much work beforehand. You will be calculating a lot of paths that might never be used. Better think about projecting into a `Dictionary` and traverse the hierarchy to find all possible paths on demand.
– Good Night Nerd Pride Apr 26 '16 at 08:46