0

我有一个对象

class item { Guid id; String name; List<item> childs;}

它的数据有两部分。

咖啡店

- 约翰

- - 盒子

——罗密欧

咖啡店

- 约翰

——罗密欧

- - 盒子

所以,盒子只是改变了它的主人。

现在我需要把这个结构作为一个字符串。

所以,它必须是:

咖啡厅,约翰,盒子,罗密欧

咖啡厅,约翰,罗密欧,盒子

我有代码:

public static IEnumerable<item> get_in_one_row(this IEnumerable<item> itemsTree)
    {
        return itemsTree.Select(p=>p).Union(itemsTree.SelectMany(p=>p.childs.get_in_one_row()));
    }

但我总是得到:cafe,John,Romeo,Box - 不管,谁是盒子的主人。

我应该怎么办?

谢谢。

4

1 回答 1

1

要按该顺序获得结果,您需要以不同的方式遍历树:

public static IEnumerable<item> get_in_one_row(this IEnumerable<item> itemsTree)
    {
        return itemsTree.SelectMany(p => Enumerable.Repeat(p,1).Concat(p.childs.get_in_one_row()));
    }
于 2013-10-06T08:20:30.737 回答