Linq to Xml查询子节点

Ard*_*ris 4 c# xml linq

<InventoryList>
 <Product xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Id>1</Id>
  <Name>Pizza Ristorante Hawaii</Name>
  <Price>2.99</Price>
  <VariableWeightPrice>€ 8,42 / kg</VariableWeightPrice>
  <Brand>Dr.Oetker</Brand>
  <PackageInfo>355 GR</PackageInfo>
  <categoryString />
  <PictureSmallFilename>1small.jpg</PictureSmallFilename>
  <InformationTakenFrom>Jumbo</InformationTakenFrom>
  <MarketItemUrl></MarketItemUrl>
  <BarCode>4001724819608</BarCode>
  <IsBlackListed>false</IsBlackListed>
  <ItemLists>
    <Item>
        <ListName>in</ListName>
        <Quantity>1</Quantity>
        <QuantityWeight>0</QuantityWeight>
    </Item>
    <Item>
        <ListName>out</ListName>
        <Quantity>2</Quantity>
        <QuantityWeight>0</QuantityWeight>
    </Item>
  </ItemLists>
 </Product>
 <Product xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Id>2</Id>
  <Name>Produto 2</Name>
  <Price>2.99</Price>
  <VariableWeightPrice>€ 5,55 / kg</VariableWeightPrice>
  <Brand>Dr.Oetker</Brand>
  <PackageInfo>355 GR</PackageInfo>
  <categoryString />
  <PictureSmallFilename>1small.jpg</PictureSmallFilename>
  <InformationTakenFrom>Jumbo</InformationTakenFrom>
  <MarketItemUrl></MarketItemUrl>
  <BarCode>4001724819608</BarCode>
  <IsBlackListed>false</IsBlackListed>
  <ItemLists>
    <Item>
        <ListName>out</ListName>
        <Quantity>1</Quantity>
        <QuantityWeight>0</QuantityWeight>
    </Item>
  </ItemLists>
</Product>
</InventoryList>
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助。我有这个xml数据库

我想返回所有具有ListName =“ out”的产品,但是此查询正在尝试仅返回第二个产品,并且我需要它返回第一和第二个产品。

 var _queryItems = from c in xml.Descendants("Product")
                          where
                              c.Element("ItemLists").Element("Item").Element("ListName").Value == "out"
                          select c;
Run Code Online (Sandbox Code Playgroud)

谢谢 :)

Bro*_*ass 5

现在,您只需要检查第一个Item元素,而是要检查是否有任何项目的ListName匹配项“ out”:

var _queryItems = from c in xml.Descendants("Product") 
                  where c.Element("ItemLists")
                         .Elements("Item").Any( x=> x.Element("ListName").Value == "out") 
                  select c;
Run Code Online (Sandbox Code Playgroud)