I'm writing access methods that query data from a database that return an IQueryable object. If something goes wrong, though, I don't want to return null, but rather an empty list. I thought I had the following working:
You can call .AsQueryable() on an IEnumerable<T> sequence and it will return IQueryable<T>.
But I would seriously question why you'd want to do this. Returning an empty list in case of an exception is almost always a bad pattern - if an unknown exception is thrown, you really want to let that exception bubble up.
You can call .AsQueryable() on an IEnumerable<T> sequence and it will return IQueryable<T>.
But I would seriously question why you'd want to do this. Returning an empty list in case of an exception is almost always a bad pattern - if an unknown exception is thrown, you really want to let that exception bubble up.
It's not always in that case, it was just the example case. Often I call these methods in a foreach loop and returning null causes an exception.
I do use the above pattern for my generated code, but in that case I allow the user to define a delegate for exceptions, meaning they can trap, log, and/or throw the exception as they please. I just don't want their code to break if they do with to keep the exception suppressed, which is why I wanted the empty list.