How To Test If Linq Query Returned Results
-
I have this:
var query = from g in root.Groups
from r in g.Rules
from c in r.Conditions
from a in c.Actions
where g.Rules != null
where r.Conditions != null
where c.Actions != null
where a.ActionID == ActionId
select a;List<RuleAction> items = query.ToList();
It fails on the last line with "Object reference not set to an instance of an object" because the query returned no results. How do I test for this before attempting to convert the results to a list? Thanks
Everything makes sense in someone's mind
-
I have this:
var query = from g in root.Groups
from r in g.Rules
from c in r.Conditions
from a in c.Actions
where g.Rules != null
where r.Conditions != null
where c.Actions != null
where a.ActionID == ActionId
select a;List<RuleAction> items = query.ToList();
It fails on the last line with "Object reference not set to an instance of an object" because the query returned no results. How do I test for this before attempting to convert the results to a list? Thanks
Everything makes sense in someone's mind
I believe you have other problems. Linq is smart enough to know to create an empty list when the query doesn't return any results.
I know the language. I've read a book. - _Madmatt
-
I have this:
var query = from g in root.Groups
from r in g.Rules
from c in r.Conditions
from a in c.Actions
where g.Rules != null
where r.Conditions != null
where c.Actions != null
where a.ActionID == ActionId
select a;List<RuleAction> items = query.ToList();
It fails on the last line with "Object reference not set to an instance of an object" because the query returned no results. How do I test for this before attempting to convert the results to a list? Thanks
Everything makes sense in someone's mind
The exception is thrown by one of the
from
clauses - because thewhere
clauses are all executed after all thefrom
clauses, so fail to excluded parent classes that have null collections. Try this instead:var query = from g in root.Groups where g.Rules != null from r in g.Rules where r.Conditions != null from c in r.Conditions where c.Actions != null from a in c.Actions where a.ActionID == ActionId select a;
-
I have this:
var query = from g in root.Groups
from r in g.Rules
from c in r.Conditions
from a in c.Actions
where g.Rules != null
where r.Conditions != null
where c.Actions != null
where a.ActionID == ActionId
select a;List<RuleAction> items = query.ToList();
It fails on the last line with "Object reference not set to an instance of an object" because the query returned no results. How do I test for this before attempting to convert the results to a list? Thanks
Everything makes sense in someone's mind
Just reading this and thought I would jump in. I typically use query.FirstorDefault to verify it. You could also use query.Count>0 However, I tried some sample code and got that message when I hadn't instantiated my datacontext properly. That may not be it but just something to verify.
-
Just reading this and thought I would jump in. I typically use query.FirstorDefault to verify it. You could also use query.Count>0 However, I tried some sample code and got that message when I hadn't instantiated my datacontext properly. That may not be it but just something to verify.
If all you are doing is checking for any result, using query.Any() would be better than query.Count() since Any will stop after one item instead of always iterating the whole sequence.
-
If all you are doing is checking for any result, using query.Any() would be better than query.Count() since Any will stop after one item instead of always iterating the whole sequence.
Thanks Gideon - I hadn't used Any for that purpose. Awesome tip!