【问题标题】:2 GroupJoin's in a single query2 个 GroupJoin 在一个查询中
【发布时间】:2012-11-03 08:31:38
【问题描述】:

我有一个这样的查询,它将两个实体连接在一起:

IEnumerable<ICD.ViewModels.HomeSearchViewModel> query =
ICDUnitOfWork.AlphaGroups.Find().GroupJoin(ICDUnitOfWork.Alphas.Find(),
                                           a => a.AlphaGroupID,
                                           g => g.AlphaGroupID,
                                           (alphaGroups, alphas) =>
                                           new ICD.ViewModels.HomeSearchViewModel
                                           {
                                               AlphaGroups =
                                                   alphaGroups,
                                               Alphas = alphas,
                                               SearchTerm = searchTerm
                                           }).OrderBy(x => x.AlphaGroups.Title);

我需要将 Alphas 实体与另一个名为“代码”的实体连接起来。是否可以在单个查询中执行此操作,还是应该将其分解为 2 个单独的查询?

【问题讨论】:

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


    【解决方案1】:

    标准实体框架查询看起来更像:

    var query =
      from a in dbContext.Alphas
      from g in a.AlphaGroups
      from c in a.Codes
      select new {Alpha = a, AlphaGroup = g, Code = c}
    

    或许:

    var query =
      dbContext.Alphas.Include("AlphaGroups").Include("Codes");
    

    为什么您的查询看起来如此糟糕?

    【讨论】:

    • 我的查询不好,我应该感觉不好?
    • 别难过。对引入更多桌子的更多方式持开放态度。这个问题并没有使表格之间的关系足够清晰,无法在这些方式之间进行选择。根据提供的信息,我无法推荐 GroupJoin。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 2023-01-13
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    相关资源
    最近更新 更多