When building an MVC site, what is the best method to test that an item has been returned by a DocumentProvider?
Is the best method to use try/catch like I have below:
//Get Kentico Object
//Kentico Object with Retrieve CCB data and store in the CcbEventData property
var ev = (from e in CcbEventProvider.GetCcbEvents().Published()
where e.EventName == eventName
catch (InvalidOperationException ex)
throw new HttpException(404, "Event Not Found");
I just want to be sure I'm handling this the best way.
I could always call Count() first, but then I'm technically making two queries, which seems to add unnecessary overhead.
can't you use .Any() ? This is a LINQ function and it doesn't iterate the list like the .Count() does. So it's performance is way better then .Count().
Wouldn't that still make two database calls, though? That's still what I'm trying to avoid.
When you have your list, the .Any() function does an in memory check in the list (the list is in memory) so there is no extra database call
Thanks, Dennis. I didn't know that.
Please, sign in to be able to submit a new answer.