Linq在查询中检索摘要列表

时间:2014-06-30 10:52:10

标签: c# asp.net linq

我有一个查询

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

2 个答案:

答案 0 :(得分:1)

===编辑===

我误解了你的问题。这是更正后的答案

==== / EDIT ===

假设name是语言的名称

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

答案 1 :(得分:0)

首先进行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});