1
나는 비행 세그먼트 정보를 가지고 있고 비행 번호 정보와 세그먼트의 직교 갖고 싶어 :키핑 참조 형제
A1 A2
A1 을 :
class FlightSegment{
public string FlightNumber {get;set;}
}
class Flight{
public FlightSegment FlightSegment {get;set;}
public List<string> FlightClass {get;set;}
}
class FlightSegmentAndFlight{
public string FlightSegmentName {get;set;}
public string FlightNumberName {get;set;}
}
static class Utils {
//util for make cartesian of segments
public static IEnumerable<IEnumerable<T>> CartesianItems<T>(this IEnumerable<IEnumerable<T>> sequences) {
IEnumerable<IEnumerable<T>> emptyProduct =
new[] { Enumerable.Empty<T>() };
IEnumerable<IEnumerable<T>> result = emptyProduct;
foreach (IEnumerable<T> sequence in sequences) {
result = from accseq in result from item in sequence select accseq.Concat(new[] { item });
}
return result;
}
}
void Main()
{
var f1 = new Flight(){
FlightSegment = new FlightSegment{FlightNumber = "FN1"},
FlightClass = new List<string> {"A1","B1"}
};
var f2 = new Flight{
FlightSegment = new FlightSegment{FlightNumber = "FN2"},
FlightClass = new List<string> {"A2","B2"}
};
var flights = new List<Flight>{f1,f2};
var result = flights.Select(x => x.FlightClass).CartesianItems();
Console.WriteLine(result);
}
결과를 B2
B1 A2
,B1 B2
내가
A1, FN1
을하고 싶은 무엇 A2, Fn2를
A1, FN1
B2, Fn2를
B1, FN1
A2, FN2
B1, FN1
B2, FN2
wcf 참조에서 나오는 기존 클래스의 속성을 추가 할 수 없습니다. 세그먼트를 결합하는 동안 항공편 번호 정보를 유지하려면 어떻게해야합니까? 이 같은 그것에 대해 익명 클래스를 사용, 당신이 원하는 모든 단지 비행 클래스에 항공편 번호를 부착하기 때문에
var result2 = flights.SelectMany(f => f.FlightClass, (f, flightSegments) => new {f, flightSegments}).
Select(x=> new {
x.flightSegments.CartesianItems(),
x.f
});
과
아마도 단지 "VAR 결과 flights.Select = (X => x.FlightClass.Select (FC => 새로운 {FlightClass = FC, FlightNumber = x.FlightSegment.FlightNumber})) CartesianItems()." ? 각 클래스에 FlightNumber에 대한 정보를 추가하기 만하면됩니다. – Evk
@Evk 이것은 작동합니다 :) 대답 –
다음과 같이하십시오 : var result = string.Join ("\ n", flights.Select (x => x.FlightClass) .Select (x => string.Format ("(x [0], x [1])) .ToList()); Console.WriteLine (result); – jdweng