user1503496
user1503496

Reputation: 191

Item in a list of a list of a list is not being displayed in the console

The third foreach statement tells me that it can not convert System.Collections.Generic.IEnumerable<string> to String. ID CTAF is displayed, nomClient is displayed but numCompte is not displayed. How can I solve this?

Here is the code :

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);
                }
            }
}

Upvotes: 1

Views: 42

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236248

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);
        }
    }
}

NOTE: You have error, because f.Clients.Select(y => y.ComptesClient.Select(z => z.NumCompte)) returns sequence of string sequences, i.e. IEnumerable<IEnumerable<string>>. So, when you are trying to enumerate results of this query, you will get items of type IEnumerable<string> instead of simple string.

Another issue in your codde is that you have selected all ComptesClient of f. But you should load only data related to current client.

Upvotes: 2

Related Questions