【问题标题】:Returning empty IQueryable<>返回空的 IQueryable<>
【发布时间】:2012-12-13 20:19:33
【问题描述】:

我有这个方法试图获取事物列表:

 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>(?????);
                }
            }

        }

如果由于某种原因我无法运行查询,我想返回一个空的 IQueryable。我不想返回 NULL,因为这可能会破坏调用代码。这有可能还是我完全错了?

【问题讨论】:

    标签: entity-framework


    【解决方案1】:

    这些答案很好并且确实有效,但是我一直觉得使用 Empty 而不是创建新列表更干净:

    Enumerable.Empty<Thing>().AsQueryable();
    

    【讨论】:

    • 这是一个很好的答案。
    • 但请注意,如果您将 await / async 与 EF6 一起使用,您最终会遇到以下异常:msdn.microsoft.com/en-us/data/dn313107.aspx - 检查此链接以了解异步方式 - stackoverflow.com/questions/33305495/…
    • 对我来说,这个解决方案不适用于复杂的查询。 EF Core 抛出 InvalidOperationException 并带有消息 Processing of the LINQ expression XXX by 'NavigationExpandingExpressionVisitor' failed. This may indicate either a bug or a limitation in EF Core。我刚刚使用了.Where(x =&gt; false),它起作用了。
    【解决方案2】:

    尝试以下方法:

    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 如果没有抛出异常,你永远不会返回任何东西。也许你打算把你的回报放在最后?
    【解决方案3】:

    我会添加块 finally {} 并将我的返回类型放入该代码中。

    这将通过返回您的应用程序期望的类型来解决问题。

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

    【讨论】:

    • 绝对同意,应该以某种方式处理异常,然后在finally块中返回返回空列表的调用。
    • 谢谢,这只是一种更具防御性的编码风格:)
    【解决方案4】:

    返回空的 IQueryable DbSet.Take(0)

    【讨论】:

    • 你给答案加个解释?
    • 虽然您可能已经解决了这个用户的问题,但纯代码的答案对以后遇到这个问题的用户没有多大帮助。请编辑您的答案,以解释您的代码解决原始问题的原因。
    【解决方案5】:

    我认为这会更整洁:

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

    【讨论】:

      猜你喜欢
      • 2010-10-17
      • 1970-01-01
      • 1970-01-01
      • 2019-12-25
      • 2017-04-02
      • 2020-12-08
      • 1970-01-01
      • 1970-01-01
      • 2011-02-04
      相关资源
      最近更新 更多