Xlinq query returning null WhereEnumerableIterator?

If you have "empty" or "not existing" price elements it will break Try this: public static IEnumerable Filter(int min = 0, int max = int. MaxValue) { Func parse = p => { var element = p. Element("Price"); if (element == null) { return null; } int value; if (!Int32.

TryParse(element. Value, out value)) { return null; } return value; }; IEnumerable selected = ( from x in xmlFile. Elements("Product") let value = parse(x) where value >= min && value.

Element("Stock")" isn't right because I use an XElement in xmlFile, not XDocument. So I don't have to put the root, Stock. I figured out that the "Elements("Product")" statement is failing, it returns a null System.Xml.Linq.XContainer.

Something on your mind? And thanks for your response! – tinchou Feb 21 at 0:05.

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: public XElement filter(int min = 0, int max = int. MaxValue) { XElement filtered = new XElement("Stock"); foreach (XElement product in xmlFile.

Elements("Product")) { if ((int)product. Element("Price") >= min && (int)product. Element("Price") Add(product); } return filtered; } that would be great if someone gives me an explain.

Thanks for reading.

What you were seeing in the debugger is normal because the LINQ query had not been enumerated yet. And your binding failed because you did not wrap it in the Stock XElement. – sixlettervariables Oct 7 at 23:10.

You have two issues: When you hovered over the WhereEnumerableIterator and saw . Current was null, everything was working normally. This is deferred execution at work.

Some LINQ queries (this applies to XLinq too) do not execute until you enumerate them, hence . Current will be null until you use it! When you used foreach in your answer it enumerated the iterator and produced a value.

Your initial code did not work as it returned an enumeration of XML without a root element, and it appears whatever your calling code is it required it to have a root. Your answer wraps the data in a element. You can use your original code like so: public XElement Filter(int min = 0, int max = int.

MaxValue) { var selected = ( from x in xmlFile. Elements("Product") where (int)x. Element("Price") >= min && (int)x.

Element("Price").

Thank you, @sixlettervariables! – tinchou Oct 12 at 2:15.

I'm trying to do a filtering xlinq query in c# 4.0, so I can bind to a DataContext. XmlFile is an XElement with an external xml file loaded. 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. If I just return the xmlFile, it goes fine, but I really need to do the filtering!

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions