Linq在查询中检索摘要列表

本文关键字:列表 检索 查询 Linq | 更新日期: 2023-09-27 18:10:51

我有一个查询

var query =
    from p in db.Person
    join l in db.PersonLanguage on p.personId equals l.personId
    select new (p, **as a single field I want a list of languages**)

我想知道这是否可能,或者我需要迭代结果并进行单独的查询,以便我可以获得列表。理想情况下,我后面的列表用空格分隔,但任何分隔符都可以使用。

列表看起来像

English Spanish French

Linq在查询中检索摘要列表

=== EDIT ==

我误解了你的问题。以下是正确答案

= = = =/编辑= = =

假设名称是语言的名称

var query = 
    from pair in 
    (from p in db.persons
    join l in db.personLanguages on p.personId equals l.personId
    select new {p, l})
group pair by pair.p into g
select new {Person = g.Key , Langs = string.Join(" " ,g.Select(gr=> gr.l.name).ToArray())}

先做GroupBy再加入

var l = db.PersonLanguage.GroupBy(x => x.personId)
    .Select(x => new {gPersonId = x.Key, languages = x.Select(z=>z.LanguageId)})
    .Join(db.Person, a=> a.gPersonId, b=> b.personId, (a, b) => new {person = b, languages = a.languages});