0

我相信我需要在单个 linq 查询中有多个“Group into”语句。这是我正在尝试做的事情:

var queryNew = from a in ICDUnitOfWork.AlphaGroups.Find()
               join e in ICDUnitOfWork.Alphas.Find()
                      on a.AlphaGroupID equals e.AlphaGroupID into g
               join c in ICDUnitOfWork.Codes.Find()
                      on a.CodeID equals c.CodeID into co
               join cod in ICDUnitOfWork.Codes.Find()
                        on g.CodeID equals cod.CodeID
               select new HomeSearchViewModel
               {
                   Alphas = g,
                   AlphaGroups = a,
                   AlphaGroupCode = co,
                   AlphasCodes = cod
               };

一个 Alpha 组具有一组 Alpha。每个 AlphaGroup 都有一个对应的 AlphaGroup.CodeId,每个 Alpha 都有一个 Alpha.CodeId 需要加入“代码”类。我不知道如何调整我的查询来完成这个。

编辑: 最终结果应该类似于:

AlphaGroup -> 包含多个“Alpha”

每个 AlphaGroup 都有一个来自“Codes”类的对应 CodeId,每个 Alpha 也有一个对应的 CodeID。

所以 ViewModel 看起来像:

public class HomeSearchViewModel
{
    public IEnumerable<Alpha> Alphas { get; set; }
    public AlphaGroup AlphaGroups { get; set; }
    public IEnumerable<Code> AlphasCodes { get; set; }
    public Code AlphaGroupCode { get; set; }
    public string SearchTerm { get; set; }
}
4

2 回答 2

0

完全未经测试,但尝试类似:

select new
    {
      Alphas = g
      AlphaGroups = a,
      AlphaGroupCode = co,
      AlphasCodes = cod
    }
 .GroupBy(grp => grp.AlphaGroups)
 .Select(grp => new HomeSearchViewModel
    {
       AlphaGroups = grp.Key,
       AlphaGroupCode = grp.Key.Code,
       Alphas = grp.ToList(),
       AlaCodes = grp.Select(alp => alp.Code)
    });
于 2012-11-16T17:05:15.397 回答
0

在这种情况下,将其置于流畅的语法中可能会更容易,如果我了解您的要点,您正在尝试执行以下操作:

var result = ICDUnitOfWork.AlphaGroups.Find()
                          .GroupJoin( ICDUnitOfWork.Alphas.Find()
                                                   .Join( ICDUnitOfWork.Codes.Find(),
                                                          alpha => alpha.CodeID,
                                                          code => codeID,
                                                          (alpha,code) => new
                                                                    {
                                                                      Alpha = alpha,
                                                                      AlphaCode = code
                                                                    }),
                                       alphaG => alphaG.AlphaGroupID,
                                       anon => anon.Alpha.AlphaGroupID,
                                       (alphaG, anonG) => new
                                                       {
                                                        AlphaGroup = alphaG,
                                                        AnonG = anonG
                                                       })
                          .Join( ICDUnitOfWork.Codes.Find(),
                                 anon => anon.AlphaGroup.CodeID,
                                 code => code.CodeID,
                                 (anon, g) => new HomeSearchViewModel()
                                              {
                                                     AlphaGroups = anon.AlphaGroup,
                                                     AlphaGroupCode = g,
                                                     Alphas = anon.AnonG
                                                                  .Select(anonG => anonG.Alpha),
                                                     AlphaCodes = anon.AnonG
                                                                      .Select(anonG => anonG.AlphaCode)
                                                   });

因此,这是在假设 alpha 的代码是一对一的,或者您不希望它们的嵌套组(这似乎是您从视图结构而不是您的查询中所说的......)的假设下工作的。

同样,将它放在静态语法中会是这样的:

var result = from ag in (from AlphaG in ICDUnitOfWork.AlphaGroups.Find()
                                 join AlphaGCode in ICdUnitOfWork.Codes.Find()
                                 on AlphaG.CodeID = AlphaGCode.CodeID)
             join a in (from Alpha in ICDUnitOfWork.Alphas.Find()
                                 join AlphaCode in ICdUnitOfWork.Codes.Find()
                                 on Alpha.CodeID = AlphaCode.CodeID)
             into Alphas
             select new HomeSearchViewModel
                    {
                     AlphaGroups = ag.AlphaG,
                     AlphaGroupCode = ag.AlphaGCode,
                     Alphas = Alphas.Select(anon => anon.Alpha),
                     AlphasCodes = Alphas.Select(anon => anon.AlphaCode)
                    };

虽然这可能需要一些调整,因为无法访问 vs ATM。

于 2012-11-16T23:49:12.707 回答