さて、配列として別の POCO クラスを含む可能性のある POCO クラスがあります。場合によっては、データを取得するときに、リストのリストを作成したいのですが、レベルを下げるのではなく、すべて同じレベルにします。非常に単純なものが欠けていると思うので、ここで質問すると思いました。ラムダのさまざまな構文を試し続けていますが、データはそこにあります。それを一番上に表示させることはできません。古い学校のforeachを行う代わりに、可能であれば解決策をラムダにしたいと思います。これをインラインで実行できるかどうか、または最初にコレクションを宣言してから追加する必要があるかどうかはわかりませんでした。私がいる場所:
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);
}
}