代码之家  ›  专栏  ›  技术社区  ›  Bullines

Linq问题:组合式何处条款

  •  3
  • Bullines  · 技术社区  · 17 年前

    <Root>
        <SectionA>
            <Item id="111">
                <Options>
                    <Option val="a" cat="zzz">
                        <Package value="apple" />
                        <Feature value="avacado" />
                    </Option>
                    <Option val="b" cat="yyy">
                        <Package value="banana" />
                        <Feature value="blueberry" />
                    </Option>
                </Options>
            </Item>
            <Item id="222">
                <Options>
                    <Option val="c" cat="xxx">
                        <Package value="carrot" />
                        <Feature value="cucumber" />
                    </Option>
                    <Option val="d" cat="www">
                        <Package value="dairy" />
                        <Feature value="durom" />
                    </Option>
                </Options>
            </Item>
        </SectionA>
        <SectionB>
        .
        .
        .
        </SectionB>
    </Root>
    

    我想根据ITEM的ID属性为“111”和OPTION的VAL属性为“a”来获取包和特征值。

    4 回复  |  直到 17 年前
        1
  •  7
  •   Samuel    17 年前

    这对我有效。

    var doc = XDocument.Parse(s);
    
    var items = from item in doc.Descendants("Item")
                where item.Attribute("id").Value == "111"
                from option in item.Descendants("Option")
                where option.Attribute("val").Value == "a"
                let package = option.Element("Package").Attribute("value")
                let feature = option.Element("Feature").Attribute("value")
                select new { Package = package.Value, Feature = feature.Value };
    
    items.First().Feature; // = "avacado"
    items.First().Package; // = "apple"
    

    您可以省略 let 如果你愿意,它们只是为了使匿名类型更薄。

    var items = from item in doc.Descendants("Item")
                where item.Attribute("id").Value == "111"
                from option in item.Descendants("Option")
                where option.Attribute("val").Value == "a"
                select new
                {
                   Package = option.Element("Package").Attribute("value").Value,
                   Feature = option.Element("Feature").Attribute("value").Value
                };
    

    事实上,我更喜欢第二个。


    以及非查询式的Linq风格。

    var items = doc.Descendants("Item")
                   .Where(item => item.Attribute("id").Value == "111")
                   .SelectMany(item => item.Descendants("Option"))
                   .Where(option => option.Attribute("val").Value == "a")
                   .Select(option => new
                   {
                    Package = option.Element("Package").Attribute("value").Value,
                    Feature = option.Element("Feature").Attribute("value").Value
                   });
    
        2
  •  4
  •   Orion Edwards    17 年前

    var doc = XDocument.Parse(xml);
    var items = from i in doc.Descendants("Item")
                from o in i.Descendants("Option")
                where i.Attribute("id").Value == "111"
                   && o.Attribute("val").Value == "a"
             select new {
                 Package = i.Descendants("Package").Attribute("value").Value,
                 Feature = i.Descendants("Feature").Attribute("value").Value                     
             };
    
        3
  •  0
  •   Aaron Powell    17 年前

    以下是一种自下而上的方法:

    var items = xdoc
                .Descendants("Option")
                .Where(o => (string)o.Attribute("val") == "a" && (int)o.Ancestors("Item").Single().Attribute("id") == 111)
                .Select(o => new { 
                  Package = o.Element("Package"), 
                  Feature= o.Element("Feature") 
                });
    
        4
  •  0
  •   Shimmy Weitzhandler 500 - Internal Server Error    17 年前

    Module Module1
    
        Sub Main()
            Dim xml As XElement = <Root>
                                      <SectionA>
                                          <Item id="111">
                                              <Options>
                                                  <Option val="a" cat="zzz">
                                                      <Package value="apple"/>
                                                      <Feature value="avacado"/>
                                                  </Option>
                                                  <Option val="b" cat="yyy">
                                                      <Package value="banana"/>
                                                      <Feature value="blueberry"/>
                                                  </Option>
                                              </Options>
                                          </Item>
                                          <Item id="222">
                                              <Options>
                                                  <Option val="c" cat="xxx">
                                                      <Package value="carrot"/>
                                                      <Feature value="cucumber"/>
                                                  </Option>
                                                  <Option val="d" cat="www">
                                                      <Package value="dairy"/>
                                                      <Feature value="durom"/>
                                                  </Option>
                                              </Options>
                                          </Item>
                                      </SectionA>
                                      <SectionB>                                      
                                      </SectionB>
                                  </Root>
    
            Dim data = From x In xml...<Option> _
                       Where x.Ancestors("Item").@id = "111" AndAlso x.@val = "a" _
                       Select Package = x.<Package>.@value, _
                              Feature = x.<Feature>.@value
    
            For Each item In data
                Console.WriteLine("Package: {0}, Feature: {1}", item.Package, item.Feature)
            Next
            Stop
        End Sub
    
    End Module