i'm trying to do a filtering xlinq query in c# 4.0, so i can bind to a DataContext. the code is this:
public IEnumerable<XElement> Filter(int min = 0, int max = int.MaxValue)
{
IEnumerable<XElement> selected = (
from x in xmlFile.Elements("Product")
where (int)x.Element("Price") >= min &&
(int)x.Element("Price") <= max
select x);
return selected;
}
xmlFile is an XElement with an external xml file loaded. the xml file structure looks something like this:
<Stock>
<Product>
<Name />
<Price />
<Quantity />
</Product>
...
</Stock>
I don't get any errors when building or running, but the selected
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!
EDIT
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):
(It won't let me insert an image because i'm a new user, but it es here: https://i.stack.imgur.com/XTt8r.png
I use this function in my code as a DataContext like this:
gridpricelist.DataContext = Conn.Filter(min: Convert.ToInt32(minprice.Text),
max: Convert.ToInt32(maxprice.Text));
minprice and maxprice are textboxes, and a KeyUp event triggers the function from above
EDIT2
I figured it out, see my answer. But I can't explain why it works that way, could someone help me understand?
Thanks in advance!
I have figured it out! Although I don't know why the Xlinq approach doesn't work... here is the code that worked for me instead of the query:
that would be great if someone gives me an explain. thanks for reading