未显示列表列表中的项目

时间:2014-01-23 11:11:44

标签: c# .net list linq-to-xml

enter image description here

它显示了idfichier,而nomclient显示了system.linq.enumerable ... 我猜它显示了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 ));
            }

        }

我该如何显示?图片显示了我得到的结果

2 个答案:

答案 0 :(得分:0)

而不是使用SelectMany使用Select

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

更新

我遇到了你的问题。如果您使用Select,则会返回IEnumberable<String>,因此您需要迭代才能打印NomClient的值。

目前您没有循环打印多个值,下面是以逗号分隔打印值的示例。

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

使用以下行:

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

答案 1 :(得分:0)

你看到奇怪的System.Linq.Enumerable+WhereSelectListIterator,因为它是列表迭代器的ToString()表示,由f.Clients.Select(y => y.NomClient)查询返回。

如果您需要显示所有NomClient值,我建议您构建它们的连接字符串:

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

或者您可以枚举NomClients值并在各自的行上打印:

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