0
好吧,我有一個POCO類可能包含另一個POCO類作爲數組。在某些情況下,當我獲得數據時,我想要創建列表的列表,但不是降級,但都處於同一級別。我想我錯過了很簡單的東西,所以我想我會在這裏問。我一直嘗試使用不同的語法來處理Lambdas,但數據在那裏,我可以永遠不會讓它出現在頂部附近。如果可能的話,我希望解決方案能夠在lambda表達式中,而不是做舊式的foreach。我不確定你是否可以內聯這樣做,或者你必須首先聲明一個集合,然後添加到它。當我在:使用lambda表達式在Linq的列表中創建一個新的通用列表?
class Program
{
public class lowerlevel
{
public string ChildName;
}
public class upperlevel
{
public string ItemName;
public lowerlevel[] ChildNames;
}
static void Main(string[] args)
{
// Create a list of a POCO object that has lists in it as well.
List<upperlevel> items = new List<upperlevel>
{
// declaration of top level item
new upperlevel
{
ItemName = "FirstItem",
// declaration of children
ChildNames = new lowerlevel[]
{new lowerlevel {ChildName = "Part1"}, new lowerlevel {ChildName = "Part2"}},
},
// declaration of top level item
new upperlevel
{
ItemName = "SecondItem",
// declaration of children
ChildNames = new lowerlevel[] { new lowerlevel { ChildName = "Part3" } }
}
};
var stuff = items.Select(l1 => l1.ChildNames.ToList().Select(l2 =>
new lowerlevel
{
ChildName = l2.ChildName
}))
.ToList();
// Arghh! I just want to make a new list with lambdas that is NOT nested a level down! This is NOT what I want but it is valid.
stuff.ForEach(n => n.ToList().ForEach(n2 => n2.ChildName));
// I want this but it does not work as I am not doing the expression right
// stuff.Foreach(n => n.ChildName);
}
}
我想的SelectMany()是你在找什麼。 – Maess 2013-05-10 18:32:24
這似乎是,如果我把它放在我的例子'項目'後。謝謝! – djangojazz 2013-05-10 19:30:03