38

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?

Otávio Décio
  • 73,752
  • 17
  • 161
  • 228

5 Answers5

91

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();
Chris Werner
  • 1,286
  • 12
  • 6
  • 1
    This is a great answer. – Irwin Aug 25 '15 at 19:29
  • 8
    But mind that if you use await / async with EF6 you will end up with following exception: https://msdn.microsoft.com/en-us/data/dn313107.aspx - Check this link for async manner - http://stackoverflow.com/questions/33305495/how-to-return-empty-iqueryable-in-an-async-repository-method – michal.jakubeczy Aug 30 '16 at 12:33
  • 6
    For me, this solution did not work in a complex query. EF Core was throwing `InvalidOperationException` with a message `Processing of the LINQ expression XXX by 'NavigationExpandingExpressionVisitor' failed. This may indicate either a bug or a limitation in EF Core`. I have just used `.Where(x => false)` which worked. – Artur Krajewski Jun 19 '20 at 11:41
10

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;
            }
        }
mreyeros
  • 4,359
  • 20
  • 24
  • @mreyeros You never return anything if no exception is thrown. Perhaps you meant to put your return in a finally? – cadrell0 Dec 13 '12 at 20:26
7

I would add block finally {} and put my return type in that code.

This will take care of the issue by returning the type that your application expects.

private static IQueryable<T> GetThings(int thingsType)
    {
            IQueryable<T> list = new List<Thing>().AsQueryable();
            try
            {
                list = from thing in entities.thing.Include("thingStuff")
                       select t;
            }
            catch (Exception exception)
            {
               // handle exception here;
            }
            finally {    
              return list;
            }

        }

    }
Yusubov
  • 5,815
  • 9
  • 32
  • 69
  • 2
    Definitely agree, the exception should be handled in some fashion or another, and then the call to return the empty list should be returned in the finally block. – mreyeros Dec 13 '12 at 20:28
  • Thanks, it just a bit more defensive style of coding :) – Yusubov Dec 13 '12 at 20:29
5

Returning empty IQueryable<> DbSet.Take(0)

Brick
  • 59
  • 1
  • 2
  • Ma you add an explanation for the answer? – Al-Mothafar Mar 02 '17 at 19:18
  • 4
    While you may have solved this user's problem, code-only answers are not very helpful to users who come to this question in the future. Please edit your answer to explain why your code solves the original problem. – Joe C Mar 02 '17 at 23:34
3

I think this would be tidier:

private static IQueryable<T> GetThings(int thingsType)
{
    try
    {
        return from thing in entities.thing.Include("thingStuff")
                   select t;
    }
    catch (Exception exception)
    {
       // Exception handling code goes here

       return new List<Thing>().AsQueryable();
    }
}
Rob King
  • 1,131
  • 14
  • 20