item in a list of a list is not beeing displayed [closed]

吃可爱长大的小学妹 提交于 2019-12-13 08:25:28

问题


it shows me the idfichier, and the nomclient shows system.linq.enumerable... I guess it is showing the type of nomclient.

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} \n Nom Client : {1}\n \n", f.IdFichierCtaf, f.Clients.Select(y => y.NomClient ));
            }

        }

How can I display that? the picture displays the result that i got


回答1:


You see that strange System.Linq.Enumerable+WhereSelectListIterator because it's ToString() representation of list iterator, which is returned by f.Clients.Select(y => y.NomClient) query.

If you need to display all NomClient values, I suggest you to build concatenated string of them:

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

    foreach (FichierCTAF f in fc)
    {
        Console.WriteLine("ID CTAF : {0}\n Nom Client : {1}\n\n", 
           f.IdFichierCtaf, 
           String.Join(", ", f.Clients.Select(y => y.NomClient)));
    }
}

Or you can enumerate NomClients values and print each on its own line:

public static void generateCTAF(string pathXml, string outputPDF)
{
    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);
    }
} 



回答2:


Rather than using SelectMany use Select

f.Clients.Select(y => y.NomClient)

Update:

I got your problem. If you use Select then it returns IEnumberable<String> so you need iterate to print the values of NomClient.

Currently you are not looping to print multiple values, below is the example to print values in comma separated.

String.Join(", ", f.Clients.Select(y => y.NomClient))

Use below line:

Console.WriteLine("ID CTAF : {0} \n Nom Client : {1}\n \n", f.IdFichierCtaf, String.Join(", ", f.Clients.Select(y => y.NomClient)));


来源:https://stackoverflow.com/questions/21306482/item-in-a-list-of-a-list-is-not-beeing-displayed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!