Question

Is it possible to change this Linq to XML statement into a compiled query ?

myxmlnodeList2 = From el In mynode.<attributwert> 
                 Where el.Attribute("AttributID") = sAttributID.ToLower And el.Attribute("Verwendung") = sVerwendung 
                 Select el

I used this method: Msdn

This works perfectly, if I only have 1 single Where condition. So, how can I create a compiled query with 2 Where conditions?

Was it helpful?

Solution

This already is a compiled query.

Quote from the MSDN:

queries in LINQ to XML are statically compiled [...]. This feature is built in to LINQ to XML, so you do not have to perform extra steps to take advantage of it

OTHER TIPS

Why are you trying to compile the LINQ to XML query beyond what the compiler does? Are you running into perf issues due to a large XML structure? If so, consider looking into the XStreamingElement implementation instead.

Also, you might want to consider using .First or .FirstOrDefault instead of Where in your query to short circuit the query. In your query, you have to iterate over the entire graph. Using the First methods, you stop evaluating once you get to the first valid match. Of course, if you do want a collection then .Where is perfectly valid.

A third thing that has nothing to do with performance, but is entirely stylistic. You may want to consider using XML Literals for both the element and attribute rather than mixing the literals with the string parameters:

Dim myAttribute = sAttributID.ToLower()    ' Pull this out to only parse once
myxmlnodeList2 = From el In mynode.<attributwert> 
                 Where el@AttributID = myAttribute
                 And el@Verwendung = sVerwendung 
                 Select el
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top