简体   繁体   English

多个组成单个linq查询

[英]multiple group into in single linq query

I believe I need to have multiple "Group into" statements in a single linq query. 我相信我需要在单个linq查询中包含多个“分组为”语句。 Here is what I am trying to do: 这是我正在尝试做的事情:

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

An alpha group has an collection of Alphas. 一个Alpha组具有一个Alpha集合。 Each AlphaGroup has a corresponding AlphaGroup.CodeId, an each Alpha has an Alpha.CodeId that needs to join with the 'Codes" class. I'm not sure how to adjust my query to accomplish this. 每个AlphaGroup都有一个对应的AlphaGroup.CodeId,每个Alpha都有一个Alpha.CodeId,需要与“ Codes”类结合使用。我不确定如何调整查询来实现此目的。

Edit: the final result should look something like: 编辑:最终结果应类似于:

AlphaGroup -> contains Multiple "Alphas" AlphaGroup->包含多个“ Alpha”

Each AlphaGroup has a corresponding CodeId from the "Codes" class, and each Alpha also has a corresponding CodeID. 每个AlphaGroup具有相应代码ID从“代码”类,并且每个阿尔法也有相应的代码ID。

So the ViewModel looks like: 因此,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; }
}

Completly untested, but try something similar to: 完全未经测试,但尝试类似的方法:

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

In this case putting it in fluid syntax may make it a lot easier, if I get your gist you are trying to do the following: 在这种情况下,使用流畅的语法可能会使它容易得多,如果您的主旨是,您尝试执行以下操作:

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

So this works under the assumption that code to alpha is one-to-one, or that you don't want nested groups of them (which is what you seem to be saying form your view structure but not your query...). 因此,这是在以下条件下进行的:假设Alpha代码是一对一的,或者您不希望它们嵌套组(这似乎是您在视图结构中说的,但不是您的查询...)。

Similarly putting it in static syntax would be something like so: 类似地,将其置于静态语法中将类似于:

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

Though this might need some tweaking as don't have access to vs ATM. 尽管这可能需要进行一些调整,因为无法访问vs ATM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM