2014-01-23 6 views
1

세 번째 foreach 문은 System.Collections.Generic.IEnumerable<string>String으로 변환 할 수 없다고 알려줍니다. ID CTAF가 표시되고 nomClient가 표시되지만 numCompte는 표시되지 않습니다. 이 문제를 어떻게 해결할 수 있습니까?목록에있는 목록 목록의 항목이 콘솔에 표시되지 않습니다.

public static void generateCTAF(string pathXml, string outputPDF) 
{ 
      List<FichierCTAF> fc = new List<FichierCTAF>(); 

      fc = getXmlFCtaf(pathXml); 

      foreach (FichierCTAF f in fc) 
      { 
       Console.WriteLine("ID CTAF : {0}", f.IdFichierCtaf); 

       foreach(string nomClient in f.Clients.Select(y => y.NomClient)) 
       { 
        Console.WriteLine(" Nom Client : {0}", nomClient); 
        foreach (string idCompte in f.Clients.Select(y => y.ComptesClient.Select(z => z.NumCompte))) 
         Console.WriteLine(" Num Compte : {0}\n", idCompte); 
       } 
      } 
} 

답변

2
public static void generateCTAF(string pathXml, string outputPDF) 
{ 
    // do not initialize fc variable with empty list 
    List<FichierCTAF> fc = getXmlFCtaf(pathXml); 

    foreach (FichierCTAF f in fc) 
    { 
     Console.WriteLine("ID CTAF : {0}", f.IdFichierCtaf); 

     foreach(var client in f.Clients) // select client here 
     { 
      // display Nom of current client 
      Console.WriteLine(" Nom Client : {0}", client.NomClient); 

      // enumerate comptes clients of current client 
      foreach (var comptesClient in client.ComptesClient)) 
       Console.WriteLine(" Num Compte : {0}\n", comptesClient.NumCompte); 
     } 
    } 
} 

참고 : 여기에

코드 당신이 오류를 가지고 있기 때문에 문자열 시퀀스의 f.Clients.Select(y => y.ComptesClient.Select(z => z.NumCompte)) 반환 순서, 즉 IEnumerable<IEnumerable<string>>. 따라서이 쿼리의 결과를 열거하려고하면 string 대신 IEnumerable<string> 유형의 항목이 표시됩니다.

코드 목록의 다른 문제는 모든 ComptesClient를 f으로 선택했다는 것입니다. 그러나 현재 클라이언트와 관련된 데이터 만로드해야합니다.

+1

하지만 'ComptesClient.NumCompte'와'IEnumerable '이 아닙니까? – Jodrell

+0

'SelectMany'를 사용하기 위해 원래 코드를 바꿀 수 없습니까? – gleng

+0

당신은 내 사람 Sergey, 당신 탱크! – user1503496

관련 문제