嵌套类列出了LINQ

时间:2014-04-04 15:16:41

标签: c# linq

我有以下课程:

public class SerializedDelegate
{
    public List<GOEntry> goEntries = new List<GOEntry>();
}
public class GOEntry
{
    public string go;
    public List<MBEntry> mbEntries = new List<MBEntry>();
}
public class MBEntry
{
    public string mb;
    public List<MethodEntry> methodsEntries = new List<MethodEntry>();
}
public class MethodEntry
{
    public string method;
}

这个数据:

var sd = new SerializedDelegate();
var eGo1 = new GOEntry { go = "Player1" };
var eGo2 = new GOEntry { go = "Player2" };
var eMb1 = new MBEntry { mb = "MB1" };
var eMb2 = new MBEntry { mb = "MB2" };
var eM1 = new MethodEntry { method = "target1" };
var eM2 = new MethodEntry { method = "target2" };
var eM3 = new MethodEntry { method = "target3" };
var eM4 = new MethodEntry { method = "target4" };
eMb1.methodsEntries.Add(eM1);
eMb1.methodsEntries.Add(eM2);
eMb2.methodsEntries.Add(eM3);
eMb2.methodsEntries.Add(eM4);
eGo1.mbEntries.AddRange(new[] { eMb1, eMb2 });
eGo2.mbEntries.Add(eMb1);
sd.goEntries.AddRange(new[] { eGo1, eGo2 });

我正在尝试做的事 - 以下列形式打印出来:

     Player1: MB1.target1
     Player1: MB1.target2
     Player1: MB2.target3
     Player1: MB2.target4
     Player2: MB1.target1
     Player2: MB1.target2

我使用foreach循环打印它们:

foreach (var goEntry in sd.goEntries) {
    foreach (var mbEntry in goEntry.mbEntries) {
        foreach (var methodEntry in mbEntry.methodsEntries)
            Console.WriteLine(goEntry.go + ": " + mbEntry.mb + "." + methodEntry.method);
    }
}

但是我想以LINQ方式实现它 - 我想构建一个与我想要打印它们的方式相对应的LINQ结构 - 这样我只需要一个foreach循环(所以构造的可枚举的长度就是数字总方法 - 在这种情况下,6)。

我是这样开始的 - 但不知道如何进一步:

var entries = sd.goEntries
    .Select(g => new
    {
        GO = g.go,
        MBs = g.mbEntries,
        Methods = g.mbEntries.SelectMany(e => e.methodsEntries)
    });

最终,这些方法条目将在弹出窗口中 - 这就是为什么我需要一个结构,我可以很容易地告诉哪个MBEntry属于哪个GOEntry,以及{{1} }属于MethodEntry

对不起,如果我不够清楚 - 如果是这样,请说出来。

感谢。

3 个答案:

答案 0 :(得分:1)

var entries = from goEntry in sd.goEntries
              from mbEntry in goEntry.mbEntries
              from methodEntry in mbEntry.methodsEntries
              select new { goEntry.go, mbEntry.mb, methodEntry.method };

您可以这样输出条目:

 foreach(var entry in entries)
    Console.WriteLine("{0}: {1}.{2}", entry.go, entry.mb, entry.method);

答案 1 :(得分:1)

听起来你想要这样的东西:

var lines = from goEntry in sd.goEntries
            from mbEntry in goEntry.mbEnties
            from methodEntry in mbEntry.methodsEntries
            select string.Format("{0}: {1}.{2}",
                                 goEntry.go, mbEntry.mb, methodEntry.method);

foreach (var line in lines)
{
    Console.WriteLine(line);
}

或者如果你想稍后进行格式化:

var entries = from goEntry in sd.goEntries
              from mbEntry in goEntry.mbEnties
              from methodEntry in mbEntry.methodsEntries
              select new { goEntry, mbEntry, methodEntry };

foreach (var entry in entries)
{
    Console.WriteLine("{0}: {1}.{2}", entry.goEntry.go,
                      entry.mbEntry.mb, entry.methodEntry.method);
}

...或根据谢尔盖的回答捕获单个值以格式化(goEntry.go等)。许多不同的方法:)

答案 2 :(得分:1)

其他人给出了更好看的答案但是由于你在其他评论中询问了它的方法版本,我在这里提出:

sd.goEntries.SelectMany(
    ge=>ge.mbEntries.SelectMany(
        mb=>mb.methodsEntries.Select(
            me=>new {me.method, mb.mb, ge.go}
        )
    )
)

与其他两个当前答案所证明的相比,查看这项工作要比查询语法风格要难得多。