Dynamically Build XML LinQ OR condition

arabgogs

Member
Joined
Nov 6, 2012
Messages
14
Location
Scotland
Programming Experience
5-10
Hi Guys,


I have been attempting to dynamically build a LinQ query and found the Where(AND) working fine, however I am now struggling to get an OR condition to work.

I have the following: -


Dim xQuery = xe in doc.descendants.elements("BLAH")

if myString <> "" Then

xQuery = xQuery.where(Function(I) i.Descendants.Elements("BLAHSUB").value = myString)
end if

I now have a list of strings I want to build an 'OR' condition from, is this possible and how can IK achieve this.


Thanks for any help



 
I guess it depends exactly what you want to OR. You could use the .Any() method on your list to compare its contents to whatever you have. You should probably explain a bit exactly what you want to do, because I do not think your current code is really what you need. Linq is not really made to be used in one-off statements, it is much easier to build a single expression to get exactly what you need.
 
Hi Herman,
Thank you for the response.
I am querying a structure akin to the below structure and I have varying elements within, this is why I would like to build the query dynamically.
The OR statement I am looking to query is based on the <Code> element of the structure, that is varying in data.

I have added the following based on your .Any() advice:
xmlElements.Where(Function(i) i.Descendants.Elements("Code").Any(Function(i2) myStringList.Contains(i2.Value)))

however I will have an issue here if I am looking for W1 and code includes a W11, this will incorrectly return both.
Can you advise of any option for the purposes of building a Where OR dynamically.

HTML:
<Struct>
  <Nested1>
    <Nested2>
      <Nested3>
        <Ele1>AAA</Ele1>
        <Ele2>BBB</Ele2>
        <Data>
          <ID>M</ID>
          <Code>L1</Code>
        </Data>
        <Ele4>R</Ele4>
      </Nested3>
    </Nested2>
    <Nested2>
      <Nested3>
        <Ele1>AAA</Ele1>
        <Ele2>BBB</Ele2>
        <Ele3>CCC</Ele3>
        <Data>
          <ID>M</ID>
          <Code>W1</Code>
        </Data>
        <Ele4>R</Ele4>
        <Ele5>Q</Ele5>
      </Nested3>
    </Nested2>
    <Nested2>
      <Nested3>
        <Ele1>AAA</Ele1>
        <Ele2>BBB</Ele2>
        <Ele3>CCC</Ele3>
        <Data>
          <ID>M</ID>
          <Code>X1</Code>
        </Data>
        <Ele4>R</Ele4>
        <Ele5>Q</Ele5>
      </Nested3>
    </Nested2>
  </Nested1>
</Struct>
 
Last edited by a moderator:
I want to select all the nested3 structures, where Code = L1 or Code = X1
For example:
Dim values = {"L1", "X1"}
Dim result = From x In doc...<Nested3> Where values.Contains(x...<Code>.Value)
 
Hi John,

Thanks for the response, that is basically what I am using already, with the .Any clause added to the statement. It is returning fine, as I required, however as I queried above, if I was to have L1 and L11 within the code element would this not return both?

I have tested it on this scenario and found that is only returning the list I have entered. I would have expected .Contains function to return values the include my list NOT only exact matches. Am I incorrect in my thinking.

Thanks again
 
I would have expected .Contains function to return values the include my list NOT only exact matches. Am I incorrect in my thinking.
Enumerable.Contains method "Determines whether a sequence contains a specified element.", there is no partial match involved in that.
 
Back
Top