0

我试图找出从两个不同元素派生的多重 where 子句。基本上,我希望能够根据 DataType & Service 元素的名称属性进行过滤。感谢任何反馈。谢谢杰

var services = from dt in doc.Descendants("DataType")
               where (string)dt.Attribute("name") == "WELL_INDUSTRY" && (string)dt.Elements("Service").Attributes == "Well_Industry"
               from service in dt.Elements("Services").Elements("Service").Elements("Layers").Elements("Layer")
               select new
               {
                   Name = (string)service.Attribute("name"),

               };

XML:

<DataTypes>
  <DataType name="WELL_INDUSTRY">
    <Spatial>
      <Services>
        <Service name="Well_Industry" group="Well" status="Primary" >
          <Layers>
            <layer name="Bottom Hole Wells" ></layer>
           <layer name="Bottom Hole Wells2" ></layer>
          </Layers>
4

2 回答 2

0

我认为您正在寻找类似的东西:

var services = from dt in doc.Descendants("DataType")
               where (string)dt.Attribute("name") == "WELL_INDUSTRY"
               from service in dt.Element("Spatial")
                                 .Elements("Services")
                                 .Elements("Service")
               where (string)service.Attribute("name") == "Well_Industry"
               from layer in service.Element("Layers")
                                    .Elements("Layer")
               select new
               {
                   ServiceName = (string)service.Attribute("name"),
                   Layers = layer.Select(x => (string)x).ToList()
               };
  1. where您可以在之后添加另一个from service ...
  2. 您仍然可以在部分查询中使用service变量。select

但是,查询之前ServiceName是否有ServiceName == "myName"检查似乎没有用。如果您需要图层名称,请使用以下内容select

               select new
               {
                   Name = (string)layes.Attribute("name")
               };
于 2013-08-22T15:03:16.877 回答
0
var services = from dt in doc.Descendants("DataType")
               where (string)dt.Attribute("name") == "WELL_INDUSTRY"
               from s in dt.Descendants("Service")
               where (string)s.Attribute("name") == "Well_Industry"
               from l in s.Descendants("Layer")
               select new { 
                    Name = (string)l.Attribute("name") 
               };

使用 XPath 也可以达到同样的效果:

var xpath = "//DataType[@name='WELL_INDUSTRY']//Service[@name='Well_Industry']//layer";
var services = from l in doc.XPathSelectElements(xpath)
               select new {
                    Name = (string)l.Attribute("name")
               };
于 2013-08-22T15:06:07.867 回答