Returning empty IQueryable<>

entity-framework

Question

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?

1
21
12/13/2012 8:19:33 PM

Accepted Answer

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();
53
8/6/2014 1:12:37 PM

Popular Answer

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;
            }
        }


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow