Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linq 无法将类型“System.Collections.Generic.IEnumerable>>”隐式转换为“System.Collections.Generic.IEnumerable”。存在显式转换_Linq_C# 4.0_Linq To Xml - Fatal编程技术网

Linq 无法将类型“System.Collections.Generic.IEnumerable>>”隐式转换为“System.Collections.Generic.IEnumerable”。存在显式转换

Linq 无法将类型“System.Collections.Generic.IEnumerable>>”隐式转换为“System.Collections.Generic.IEnumerable”。存在显式转换,linq,c#-4.0,linq-to-xml,Linq,C# 4.0,Linq To Xml,我使用下面的查询返回所有子元素,然后根据子节点获取结果 XElement rootElement = XElement.Load(@"E:\Samples\TestConsole\TestConsoleApp\TestConsoleApp\XMLFile1.xml"); IEnumerable<XElement> lv1s = from lv1 in rootElement.Descendants("A") where lv1.A

我使用下面的查询返回所有子元素,然后根据子节点获取结果

XElement rootElement = XElement.Load(@"E:\Samples\TestConsole\TestConsoleApp\TestConsoleApp\XMLFile1.xml");
        IEnumerable<XElement> lv1s = from lv1 in rootElement.Descendants("A")
                   where lv1.Attribute("Code").Value.Equals("A001") && lv1.Attribute("Lable").Value.Equals("A001")
                   select (from ltd in lv1.Descendants("A1")
                           where ltd.Attribute("Code").Value.Equals("001")
                           select ltd.Elements()).ToList();
但我仍然有以下错误

无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“System.Collections.Generic.IEnumerable”。存在显式转换。是否缺少强制转换

请让我知道

我想要我的1级[A11],1级[A22]的值

下面是我的xml

    <?xml version="1.0" encoding="utf-8" ?>
<Document>
  <A Code="A001" Lable="A001">
    <A1 Code="001">
      <A11>A1</A11>
      <A22>A2</A22>
      <A33>A3</A33>
    </A1>
  </A>
  <A Code="A002" Label="A002">
    <A1 Code="002">
      <A44>A44</A44>
      <A55>A55</A55>
    </A1>
  </A>
</Document>
请让我知道


另外,我如何处理枚举,在返回列表中没有得到计数为0的结果,因为如果发生此错误,它会给出计数>0。

建议:不要使用.Value,而是将其转换为字符串。另外,如果您的变量在任何情况下都是IEnumerable而不是List,为什么需要ToList

如果您只需要这些元素(A1、A2和A3)的值,而不是XElement对象:

查询结果的类型为IEnumerable。但您正在尝试将其分配给IEnumerable。为什么?因为对于每个A元素,您选择的是A1子元素列表。这将为您提供列表集合

您可以使用显式结果类型来解决此问题。即,使用var lvls代替IEnumerable LV1。或者使用实际lvls类型:IEnumerable lvls

或者,如果要获得IEnumerable,请使用SelectMany:

或者通过以下方式重写查询:

   IEnumerable<XElement> lv1s =  
         from lv1 in rootElement.Descendants("A")
         where (string)lv1.Attribute("Code") == "A001" &&
               (string)lv1.Attribute("Lable") == "A001"
         from ltd in lv1.Descendants("A1")
         where (string)ltd.Attribute("Code") == "001"
         from e in ltd.Elements()
         select e;
返回以下元素:

  <A11>A1</A11>
  <A22>A2</A22>
  <A33>A3</A33>
IEnumerable<XElement> lv1s = (
    from lv1 in rootElement.Elements("A")
    where (string)lv1.Attribute("Code") == "A001" && (string)lv1.Attribute("Lable") == "A001"
    from ltd in lv1.Elements("A1")
    where (string)ltd.Attribute("Code") == "001"
    from e in ltd.Elements()
    select e
);
IEnumerable<string> lv1s = (
    from lv1 in rootElement.Elements("A")
    where (string)lv1.Attribute("Code") == "A001" && (string)lv1.Attribute("Lable") == "A001"
    from ltd in lv1.Elements("A1")
    where (string)ltd.Attribute("Code") == "001"
    from e in ltd.Elements()
    select (string)e
);
IEnumerable<XElement> lv1s = 
    rootElement.Descendants("A")
               .Where(lv1 => (string)lv1.Attribute("Code") == "A001" &&
                             (string)lv1.Attribute("Lable") == "A001")
               .SelectMany(lv1 => lv1.Descendants("A1")
                            .Where(ltd => (string)ltd.Attribute("Code") == "001")
                            .Select(ltd => ltd.Elements());
   IEnumerable<XElement> lv1s =  
         from lv1 in rootElement.Descendants("A")
         where (string)lv1.Attribute("Code") == "A001" &&
               (string)lv1.Attribute("Lable") == "A001"
         from ltd in lv1.Descendants("A1")
         where (string)ltd.Attribute("Code") == "001"
         from e in ltd.Elements()
         select e;
  <A11>A1</A11>
  <A22>A2</A22>
  <A33>A3</A33>