I have this method that tries to get a list of things:
private static IQueryable<Thing> GetThings(int thingsType) { try { return from thing in entities.thing.Include("thingStuff") select thing; } catch (Exception exception) { return new EnumerableQuery<Thing>(?????); } } }
I want to return an empty IQueryable if I can't for whatever reason get the query to run. I don't want to return NULL because that could break the calling code. Is it possible or am I going totally wrong about this?
IQueryable is executed. // // Returns: // A System.Type that represents the type of the element(s) that are returned when. // the expression tree associated with this object is executed.
IQueryable<T> is a C# interface that lets you query different data sources. The type T specifies the type of the data source that you're querying. Under the hood, IQueryable uses expression trees that translate LINQ queries into the query language for the data provided.
These answers are good and do work, however I have always felt using Empty and not creating a new List is cleaner:
Enumerable.Empty<Thing>().AsQueryable();
Try the following:
private static IQueryable<Thing> GetThings(int thingsType) { IQueryable<Thing> things = new List<Thing>().AsQueryable(); try { things = from thing in entities.thing.Include("thingStuff") select thing; return things; } catch (Exception exception) { return things; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With