Return 匹配所选元素的父索引

Return Parent Index of matching Selected elements

我有一个XML如下:

<?xml version="1.0" encoding="utf-16"?>
<DataTable Uid="dtPlan">
  <Rows>
    <Row>
      <Cells>
        <Cell>
          <ColumnUid>ItemCode</ColumnUid>
          <Value>item1</Value>
        </Cell>
        <Cell>
          <ColumnUid>Price</ColumnUid>
          <Value>22</Value>
        </Cell>
      </Cells>
    </Row>
    <Row>
      <Cells>
        <Cell>
          <ColumnUid>ItemCode</ColumnUid>
          <Value>item2</Value>
        </Cell>
        <Cell>
          <ColumnUid>Price</ColumnUid>
          <Value>24</Value>
        </Cell>        
      </Cells>
    </Row>
    <Row>
      <Cells>
        <Cell>
          <ColumnUid>ItemCode</ColumnUid>
          <Value>item1</Value>
        </Cell>        
        <Cell>
          <ColumnUid>Price</ColumnUid>
          <Value>22</Value>
        </Cell>
      </Cells>
    </Row>
  </Rows>
</DataTable>

由此我想 return 那些具有匹配的 ItemCodes 的,我可以用这个查询来做:

    Dim queryResult = From x In xDoc.Descendants("Cell")
    Where x.Element("ColumnUid").Value = "ItemCode" AndAlso x.Element("Value") = "item1"
    Select x

我还想做的是 return 行元素的索引 <Row> 项目是从中选择的,所以对于上面的例子,它应该 return 1 & 3

xml 中有三个 <Row> 元素,所以如果查询匹配 <Row> 1 和 <Row> 3 中的项目,return 1 & 3(或 0 和 2)。

我试过使用:

    Dim queryResult = From x In xDoc.Descendants("Cell")
    Where x.Element("ColumnUid").Value = "ItemCode" AndAlso x.Element("Value") = "item1"
    Select x.ElementsBeforeSelf().Count()

但这只是 return 两次相同的值,有没有办法使用 linq 查询 return 行索引?

试试这个

    Dim foo As XElement
    foo = <DataTable Uid="dtPlan">
              <Rows>
                  <Row>
                      <Cells>
                          <Cell>
                              <ColumnUid>ItemCode</ColumnUid>
                              <Value>item1</Value>
                          </Cell>
                          <Cell>
                              <ColumnUid>Price</ColumnUid>
                              <Value>22</Value>
                          </Cell>
                      </Cells>
                  </Row>
                  <Row>
                      <Cells>
                          <Cell>
                              <ColumnUid>ItemCode</ColumnUid>
                              <Value>item2</Value>
                          </Cell>
                          <Cell>
                              <ColumnUid>Price</ColumnUid>
                              <Value>24</Value>
                          </Cell>
                      </Cells>
                  </Row>
                  <Row>
                      <Cells>
                          <Cell>
                              <ColumnUid>ItemCode</ColumnUid>
                              <Value>item1</Value>
                          </Cell>
                          <Cell>
                              <ColumnUid>Price</ColumnUid>
                              <Value>22</Value>
                          </Cell>
                      </Cells>
                  </Row>
              </Rows>
          </DataTable>

    Dim allCells As List(Of XElement) = (From c In foo...<Cell>
                                            Where c.<ColumnUid>.Value = "ItemCode"
                                            Select c).ToList

    Dim queryResult As IEnumerable(Of XElement)
    queryResult = From x In allCells
                    Where x.<Value>.Value = "item1"
                    Select x


    For Each el As XElement In queryResult
        Dim idx As Integer
        idx = (From x In allCells Where x.Equals(el) Select allCells.IndexOf(x) Take 1).FirstOrDefault
        Debug.WriteLine(idx.ToString)
    Next