我相信我需要在一个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需要与'Codes'类连接。我不知道如何调整我的查询来完成这个。
修改:最终结果应如下所示:
AlphaGroup - >包含多个“Alphas”
每个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; }
}
答案 0 :(得分: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)
});
答案 1 :(得分: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。