C# 控制台中未显示列表列表列表中的项目

C# 控制台中未显示列表列表列表中的项目,c#,.net,linq,C#,.net,Linq,第三个foreach语句告诉我它不能将System.Collections.Generic.IEnumerable转换为String。显示ID CTAF,显示nomClient,但不显示numCompte。我怎样才能解决这个问题 代码如下: public static void generateCTAF(string pathXml, string outputPDF) { List<FichierCTAF> fc = new List<FichierC

第三个
foreach
语句告诉我它不能将
System.Collections.Generic.IEnumerable
转换为
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);
                }
            }
}
publicstaticvoidgeneratectaf(字符串路径xml,字符串输出pdf)
{
List fc=新列表();
fc=getXmlFCtaf(路径XML);
foreach(fc中的FichierCTAF)
{
WriteLine(“ID CTAF:{0}”,f.IdFichierCtaf);
foreach(f.Clients.Select中的字符串nomClient(y=>y.nomClient))
{
WriteLine(“Nom客户端:{0}”,nomClient);
foreach(f.Clients.Select(y=>y.ComptesClient.Select(z=>z.NumCompte))中的字符串idCompte)
WriteLine(“numcompte:{0}\n”,idCompte);
}
}
}
公共静态void generateCAF(字符串路径XML,字符串输出PDF)
{
//不要用空列表初始化fc变量
List fc=getXmlFCtaf(pathXml);
foreach(fc中的FichierCTAF)
{
WriteLine(“ID CTAF:{0}”,f.IdFichierCtaf);
foreach(f.Clients中的var client)//在这里选择client
{
//显示当前客户端的名称
WriteLine(“Nom Client:{0}”,Client.NomClient);
//枚举当前客户端的comptes客户端
foreach(客户机中的var comptesClient.comptesClient))
WriteLine(“NumCompte:{0}\n”,comptesClient.NumCompte);
}
}
}
注意:您有错误,因为
f.Clients.Select(y=>y.ComptesClient.Select(z=>z.NumCompte))
返回字符串序列的序列,即
IEnumerable
。因此,当您试图枚举此查询的结果时,您将得到类型为
IEnumerable
的项,而不是简单的
string


代码中的另一个问题是您选择了
f
的所有CompteClient。但是,您应该只加载与当前客户端相关的数据。

但是,ComptesClient.NumCompte不是一个
IEnumerable
?您不能只将原始代码更改为使用
SelectMany
?@Jodrell-nope,正如您从exception
Select(z=>z.NumCompte)
返回
IEnumerable
。因此,
NumCompte
似乎是string@gleng否,因为它将加载与所有
f.Clients
相关的所有数据,而不是仅与当前“client”相关的数据
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);
        }
    }
}