[英]Linq query to orderby level then parent and child item
我查了一些有序的例子,但我仍然对最好的linq表达式感到困惑,要在代码中对程序集进行排序以生成这个列表:
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
结果需要按级别排序,但子项需要紧跟在父级之后才能列出下一个父级。 装配的组件可以是几层深。 我希望我已经充分解释了。
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
我试图实现IComparable但我没有抓住它。 仍然需要帮助。
你可以试试以下
using System;
using System.Linq;
public class Assembly
{
public int Level { get; set; }
public string Parent { get; set; }
public string Component { get; set; }
public bool Visited{get;set;} // to track visited levels
}
public class Program
{
public static void Main()
{
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" }
};
DisplayAssemblies(assemblies);
}
private static void DisplayAssemblies(Assembly[] data)
{
// order the data to be sorted by levels
var levels=data.OrderBy(t=>t.Level);
foreach(var level in levels)
if(!level.Visited)
DisplayLevel(level,data);
}
private static void DisplayLevel(Assembly level, Assembly[] data)
{
var childs=data.Where(t=>t.Parent==level.Component).ToArray();
Console.WriteLine("{0} - {1} - {2}", level.Level, level.Parent, level.Component);
level.Visited=true;
foreach(var child in childs)
{
DisplayLevel(child,data);
}
}
}
OUTPUT:
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
这是一个现场演示
希望这个能对您有所帮助
让您的Assembly
类实现IComparable
,它允许您通过实现其唯一的方法来定义它们的排序方式: CompareTo()
我认为你说你需要先按Level,然后是Parent,然后是Component。 如果是这样,你可以使用这样的东西:
class Assembly:IComparable
{
public int Level { get; set; }
public string Parent { get; set; }
public string Component { get; set; }
public int CompareTo(object obj)
{
var other = obj as Assembly;
if (this.Level != other.Level)
{
return this.Level.CompareTo(other.Level);
}
if (this.Parent != other.Parent)
{
return this.Parent.CompareTo(other.Parent);
}
return this.Component.CompareTo(other.Component);
}
}
但是,如果我误解你的逻辑,那么这个想法是具有CompareTo()
返回一个正整数,如果this
到来之前other
若是否定整数other
来之前, this
(或0
,如果他们是同等级别的)。
一旦实施了IComparable
您就可以:
query = assemblies.OrderBy(a => a);
为了按照自己的自定义顺序对Assemly
对象进行排序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.