Note that there are some explanatory texts on larger screens.

plurals
  1. POXlinq query returning null WhereEnumerableIterator<>
    text
    copied!<p>i'm trying to do a filtering xlinq query in c# 4.0, so i can bind to a DataContext. the code is this: </p> <pre><code>public IEnumerable&lt;XElement&gt; Filter(int min = 0, int max = int.MaxValue) { IEnumerable&lt;XElement&gt; selected = ( from x in xmlFile.Elements("Product") where (int)x.Element("Price") &gt;= min &amp;&amp; (int)x.Element("Price") &lt;= max select x); return selected; } </code></pre> <p>xmlFile is an XElement with an external xml file loaded. the xml file structure looks something like this:</p> <pre><code>&lt;Stock&gt; &lt;Product&gt; &lt;Name /&gt; &lt;Price /&gt; &lt;Quantity /&gt; &lt;/Product&gt; ... &lt;/Stock&gt; </code></pre> <p>I don't get any errors when building or running, but the <code>selected</code> variable gets only null (even without the where clause). When I hover the mouse over the variable when debugging, it shows the type System.Linq.Enumerable.WhereEnumerableIterator. If I just return the xmlFile, it goes fine, but I really need to do the filtering!</p> <hr> <p>EDIT</p> <p>as far as I've researched, the problem is on the "xmlFile.Elements("Product")" statement. I don't know how to explain this, so I made a screenshot (in my code I actually use "Respuesto" in place of "Products", I translated it here to make it easier):</p> <p>(It won't let me insert an image because i'm a new user, but it es here: <a href="http://i.stack.imgur.com/XTt8r.png" rel="nofollow">http://i.stack.imgur.com/XTt8r.png</a></p> <p>I use this function in my code as a DataContext like this:</p> <pre><code>gridpricelist.DataContext = Conn.Filter(min: Convert.ToInt32(minprice.Text), max: Convert.ToInt32(maxprice.Text)); </code></pre> <p>minprice and maxprice are textboxes, and a KeyUp event triggers the function from above</p> <hr> <p>EDIT2</p> <p>I figured it out, see my answer. But I can't explain why it works that way, could someone help me understand?</p> <p>Thanks in advance!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload