Linq查询到orderby级别然后是父项和子项

时间:2014-11-15 23:58:25

标签: c# linq lambda

我查了一些有序的例子,但我仍然对最好的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

我试图实现IC Comparable,但我没有抓住它。仍然需要帮助。

2 个答案:

答案 0 :(得分:2)

您可以尝试以下

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);
        }
    }
}

输出:


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

这里有一个实时DEMO

希望这会对你有所帮助

答案 1 :(得分:0)

让您的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对象进行排序