C# 合并项目时保留同级引用
我有航班和航段信息,我想有带航班号信息的笛卡尔航段:C# 合并项目时保留同级引用,c#,linq,cartesian,C#,Linq,Cartesian,我有航班和航段信息,我想有带航班号信息的笛卡尔航段: class FlightSegment{ public string FlightNumber {get;set;} } class Flight{ public FlightSegment FlightSegment {get;set;} public List<string> FlightClass {get;set;} } class FlightSegmentAndFlight{ pub
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);
}
在其中使用笛卡尔坐标因为您只需要将航班号附加到航班等级,所以请使用匿名等级,如下所示:
public static 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.Select(fc => new {FlightClass = fc, FlightNumber = x.FlightSegment.FlightNumber })).CartesianItems();
foreach (var item in result)
Console.WriteLine(String.Join(" ", item.Select(c => c.FlightClass + " " + c.FlightNumber)));
}
publicstaticvoidmain()
{
var f1=新航班()
{
FlightSegment=新的FlightSegment{FlightNumber=“FN1”},
FlightClass=新列表{“A1”,“B1”}
};
var f2=新航班
{
FlightSegment=新的FlightSegment{FlightNumber=“FN2”},
FlightClass=新列表{“A2”,“B2”}
};
var flights=新列表{f1,f2};
var result=flights.Select(x=>x.FlightClass.Select(fc=>new{FlightClass=fc,FlightNumber=x.FlightSegment.FlightNumber})).CartesianItems();
foreach(结果中的var项目)
Console.WriteLine(String.Join(“,item.Select(c=>c.FlightClass+”“+c.FlightNumber));
}
可能只是“var result=flights.Select(x=>x.FlightClass.Select(fc=>new{FlightClass=fc,FlightNumber=x.FlightSegment.FlightNumber}”)。CartesianItems();”?因此,只需向每个类添加有关FlightNumber的信息。@Evk此功能:)请回答以下问题:var result=string.Join(“\n”,flights.Select(x=>x.FlightClass)。Select(x=>string.Format(“(x={0};y={1})”,x[0],x[1])。ToList();控制台写入线(结果);
public static 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.Select(fc => new {FlightClass = fc, FlightNumber = x.FlightSegment.FlightNumber })).CartesianItems();
foreach (var item in result)
Console.WriteLine(String.Join(" ", item.Select(c => c.FlightClass + " " + c.FlightNumber)));
}