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
- Dynamically build a LINQ statement
- Create inversed datatable from object list
- Linq query - conditional dependent columns from one table in .Net MVC Core 3.1, Entity Framework
- LINQ, Join, Group By and SUM
- Dropdown does not display data on View MVC
- Linq and ASP.NET MVC: Order data from latest to previous
- Linq spit numeric list in search (kentico)
- Stored Procedure not being recognized in LINQ to SQL as a ISingleResult
- Remove similar items from List
- how to use linq with two related entities?
- Compare dates without seconds and milliseconds with LINQ in EF Core
- LINQ Entity Framework 4.2 datetime comparison fails
- Is there a way to load a dbset dynamically in EF core?
- how can this SQL be done in LINQ?
- LINQ Lambda Left join with an Inner join
- I can't get the result of a Generic List query
- LINQ toDictionary is Empty
- LINQ Confusion - Database not persisting after the application closes?
- Can I use LINQ without Entity Framework?
- Linq FirstOrDefault List inside a query
- SQL to LINQ Involving Multiple GroupJoin
- LINQ Errors out on Any() if results NULL
- Calculate a total for each record in a list based on filtered list
- Query Object's elements in c#
- LINQ group by select with collection
- Remove Item From Object SubList (LINQ)
- How to use Where with IEnumerable assignment?
- How to keep GridView's sorting state? (Ascending & Descending)
- Is there a more compact/better way to ZIP multiple lists using LINQ in C#?
- Retrieve Umbraco Tags datatype field in a LINQ query