score:1
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") <= max)
filtered.add(product);
}
return filtered;
}
that would be great if someone gives me an explain. thanks for reading
score:0
you have two issues:
when you hovered over the
whereenumerableiterator
and saw.current
wasnull
, 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 benull
until you use it! when you usedforeach
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
<stock>
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") <= max select x); return new xelement("stock", selected); }
score:2
if you have "empty" or "not existing" price elements it will break
try this:
public static ienumerable<xelement> filter(int min = 0, int max = int.maxvalue)
{
func<xelement, int?> 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<xelement> selected = (
from x in xmlfile.elements("product")
let value = parse(x)
where value >= min &&
value <= max
select x);
return arr;
}
Source: stackoverflow.com
Related Query
- Xlinq query returning null WhereEnumerableIterator<>
- Null Dapper.net query still returning Null Reference Exception with FirstOrDefault()
- EF Code First comparing null values generates strange query
- Issue with query returning null before I can check for null
- Linq query NOT returning null when empty
- LINQ query returning null for nested objects in Entity DB
- Could not evaluate expression - Linq Query just returning null
- LINQ query returning null when requesting bytes
- Checking if Var from LINQ query is Null and returning values older than x
- Returning value on null linq query
- LINQ join query returning null
- Linq query null check in data source
- Query returning argument null exception
- Include null cells in Linq query from DB in C# code
- LINQ query returning null when comparing elements in two lists
- LINQ query returning null results
- Why is this LINQ query returning null when a match exists?
- Linq Query Null check returning bool error
- LINQ returning Null while same query when write in SQL return result
- Linq Query returning null parameters when selecting whole table
- "OR " condition in where clause of linq query is returning null
- LINQ Query returning null results into anonymous variable
- returning null in linq query
- where Clause in Asp.net is returning null exception in the code below
- Linq query returning null when trying to pass a column value from list object
- Linq query where clause returning null
- Could not find an implementation of the query pattern for source type 'System.Data.Entity.DbSet'
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- how to know if my linq query returns null
- C# 6 null conditional operator does not work for LINQ query
More Query from same tag
- Perform a LINQ query without using NotMapped properties in EF
- in foreach loop, should I expect an error since query collection has changed?
- Strange Output in using SkipWhile() in LINQ
- EF: Get master-detail based on conditions on master as well as on detail
- Linq select from database table where matching property from List
- How do I test DataTable fields for NULL in a C# LINQ query?
- How to get a key value pair out of an array of objects
- How to grab letters from a word multiple times with C#/LINQ
- IQueryable<T>.Include() gets ignored
- efficiently removing data without for loop
- Exception was thrown when adding new rows to datatable
- How to get the last value from a model list without looping it in MVC?
- Linq: determine if the number of items in a sequence meets a condition
- Linq lambda query to select child and specify parent data
- Linq issue select Sum of one column minus another from different table
- Trouble converting DataTable to an Dictionary<int,int>
- How to implement an IN clause in LinQ
- sum of one column with condition using linq
- LinqToSql strange behaviour
- Is there a more effecient way to determine if two EntityCollections contain the same elements?
- LINQ to Entities Join on DateTime.DayOfWeek
- Use LINQ to read all nodes from XML
- How can I obtain objects from list with C# LINQ using boolean conditions?
- How to partition an array into multiple arrays with LINQ?
- User selected date range to weeks via linq etc
- How to extract data from dictionary by LINQ
- Linq IQueryable Generic Filter
- C# Linq to XML, get parents when a child satisfy condition
- ICommand binding causing UI memory leak in WPF application
- Return entity that has highest/lowest value in the related entity