【问题标题】:IEnumerable Items Disappear - Exist in DAL but not CallerIEnumerable 项目消失 - 存在于 DAL 但不存在调用者
【发布时间】:2012-06-02 16:24:27
【问题描述】:

我有一个检索项目的 DAL 方法。这些项目存在于 DAL 方法中,但不存在于调用代码中。这怎么可能?

调用代码:

IEnumerable<InstallationSummary> installationSummaryList =
InstallationSummaryLogic.GetByServerAppAndGroup(appServer, appWithValidGroup);

显示项目确实存在的 DAL 方法:

调用代码,不显示任何项目。他们去哪儿了?
(这是本问题顶部显示的同一行。)

DAL 方法和调用代码之间唯一的东西是一个逻辑类,它只是一个传递。为了完整起见,我将其包含在此处:

    public static IEnumerable<InstallationSummary> GetByServerAppAndGroup(ApplicationServer appServer, ApplicationWithOverrideVariableGroup appWithGroup)
    {
        return DataAccessFactory.GetDataInterface<IInstallationSummaryData>().GetByServerAppAndGroup(appServer, appWithGroup);
    }

编辑 - 显示整个 DAL 方法

public IEnumerable<InstallationSummary> GetByServerAppAndGroup(ApplicationServer appServer, ApplicationWithOverrideVariableGroup appWithGroup)
{
    IQueryable<InstallationSummary> summaries = this.Database.InstallationSummaries
        .Include(x => x.ApplicationServer)
        .Include(x => x.ApplicationWithOverrideVariableGroup.Application)
        .Include(x => x.ApplicationWithOverrideVariableGroup.CustomVariableGroup)
        .Where(x => x.ApplicationServer.IdForEf == appServer.IdForEf)
        .Where(x => x.ApplicationWithOverrideVariableGroup.Application.IdForEf == appWithGroup.Application.IdForEf);

    if (appWithGroup.CustomVariableGroup == null)
    {
        return summaries.Where(x => x.ApplicationWithOverrideVariableGroup.CustomVariableGroup == null);
    }

    return summaries
        .Where(x =>
        x.ApplicationWithOverrideVariableGroup != null &&
        x.ApplicationWithOverrideVariableGroup.CustomVariableGroup != null &&
        x.ApplicationWithOverrideVariableGroup.CustomVariableGroup.IdForEf == appWithGroup.CustomVariableGroup.IdForEf);
}

【问题讨论】:

    标签: c# linq entity-framework ienumerable iqueryable


    【解决方案1】:

    您的GetByServerAppAndGroup 方法使用Where 调用过滤summaries(我们看不到它的真正含义 - 如果您剪切并粘贴方法本身会很有帮助)。我的猜测是summaries 中的任何结果都没有通过Where 调用中的过滤器。

    【讨论】:

    • 延迟执行也会有问题吗?在他的代码中,我们会在什么时候看到实际执行的 LINQ 查询?他可能认为 DAL 有代码,因为当他询问他的集合时,调试器正在为他枚举查询。
    • @BradRem,如果您在调试器中展开结果视图,它应该会执行查询。
    • 我在问题中添加了 DAL 代码。但是,如果您在屏幕截图中注意到,我已经在 return 语句上按了 F10。请注意,结束方法大括号被突出显示。这意味着 Where 过滤器已被应用。
    • @BobHorn:但是你显示 summaries 的内容。 Where 子句不会影响这一点 - Where返回值 被过滤,但它根本不会更改
    • 我的错。让我先在变量中捕获它,然后返回它。然后我可以检查它是否确实是 Where 子句。
    猜你喜欢
    • 2014-01-06
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多