【问题标题】:EF Lazy loading - how to return only specyfic valuesEF延迟加载-如何仅返回特定值
【发布时间】:2013-10-30 16:24:35
【问题描述】:

简单的问题 - 我如何才能通过延迟加载仅返回集合的特定元素?

    public virtual ICollection<Attachment> Attachments
    {
        get
        {
            return this.Attachments.Where(x => x.del != true) as ICollection<Attachment>;
        }
        set {
            this.Attachments = value;
        }
    }

我只想返回这些&lt;Attachment&gt;,其中del != true

使用此代码,我有错误:

“System.StackOverflowException”类型的未处理异常 发生在 EntityFrameworkDynamicProxies

无法计算表达式,因为当前线程在堆栈中 溢出状态。

为什么?我该怎么做?

问候

【问题讨论】:

  • this.Attachments 指的是您的 Attachments 属性。它需要调用你的get 方法。这需要调用您的get 方法。这需要调用你的get 方法。获取 R#。它会告诉您该方法在所有路径上都是递归的。
  • 这里必须有一个支持字段。现在你处于无限递归中,直到堆栈溢出。
  • ohhhh,对我来说太早了 - 我忘了把 _Attachments 而不是 Attachments.. 无论如何谢谢大家 :)

标签: c# entity-framework asp.net-mvc-4


【解决方案1】:
public virtual ICollection<Attachment> Attachments
{
    // defines get_Attachments
    get
    {
                    // calls get_Attachments
        return this.Attachments.Where(x => x.del != true) as ICollection<Attachment>;
    }

您的get_Attachments 方法在所有路径上都是递归的。该方法将被调用,直到堆栈溢出。 This video should help you to visualise the events leading up to the stack overflowing.

您似乎没有ReSharperReSharper has a check for these kinds of mistakes。您应该安装试用版。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多