2013-05-10 2 views
0

좋아요, 그래서 배열로 다른 POCO 클래스를 포함 할 수있는 POCO 클래스가 있습니다. 어떤 경우에는 데이터를 얻을 때 목록의 목록을 만들지 만 수준은 낮추지 않고 같은 수준으로 만듭니다. 나는 아주 간단한 것을 놓치고있는 것 같아 여기서 물어볼 줄 알았다. 나는 Lambdas에 대해 다른 구문을 시도해 왔지만 데이터는 거기에있다. 나는 그것을 정상 근처에 나타나게 할 수 없다. 가능한 경우 이전 학교에서 foreach를하는 대신 솔루션을 lambdas에 보관하고 싶습니다. 이 인라인을 전혀 할 수 있는지 또는 컬렉션을 먼저 선언하고 추가해야하는지 확실하지 않았습니다. 나는에서 오전 여기서람다 식으로 된 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); 

     } 

    } 
+1

.Select()보다 사용해보십시오. – Maess

+0

내 예제에서 '항목'바로 뒤에 배치하면 그 것처럼 보입니다. 감사! – djangojazz

답변

1

내가 SelectMany()는 당신이 찾고있는 무엇을 생각 SelectMany() 오히려

var stuff = items.SelectMany... 
관련 문제