I have looked up some orderby examples but I'm still confused about the best linq expression to sort the assembly in the code to produce this list:
Level Parent Child
1 L010057501U 231-100-002
1 L010057501U 307-355-022
2 307-355-022 307-355-058
3 307-355-058 355-100-008
3 307-355-058 357-200-002
2 307-355-022 307-355-059
3 307-355-059 355-200-005
3 307-355-059 357-100-002
The results needs to be sorted by level but the child needs to be immediately after the parent before the next parent is listed. The components of the assembly could be several levels deep. I hope I have explained adequately.
class Program
{
static void Main(string[] args)
{
SortAssembly();
Console.ReadLine();
}
class Assembly
{
public int Level { get; set; }
public string Parent { get; set; }
public string Component { get; set; }
}
private static void SortAssembly()
{
Assembly[] assemblies = {
new Assembly { Level=1, Parent="L010057501U", Component="231-100-002" },
new Assembly { Level=1, Parent="L010057501U", Component="307-355-022" },
new Assembly { Level=2, Parent="307-355-022", Component="307-355-058" },
new Assembly { Level=2, Parent="307-355-022", Component="307-355-059" },
new Assembly { Level=3, Parent="307-355-058", Component="355-100-008" },
new Assembly { Level=3, Parent="307-355-059", Component="355-200-005" },
new Assembly { Level=3, Parent="307-355-059", Component="357-100-002" },
new Assembly { Level=3, Parent="307-355-058", Component="357-200-002" }
};
var query = ???
foreach (var part in query)
{
Console.WriteLine("{0} - {1} - {2}", part.Level, part.Parent, part.Component);
}
}
}
1 - L010057501U - 231-100-002
1 - L010057501U - 307-355-022
____2 - 307-355-022 - 307-355-058
________3 - 307-355-058 - 355-100-008
________3 - 307-355-058 - 357-200-002
____2 - 307-355-022 - 307-355-059
________3 - 307-355-059 - 355-200-005
________3 - 307-355-059 - 357-100-002
I tried to implement the IComparable but I'm not grasping it. Still need help on this.