Linq2EF:将另一个表中的所有值Concat转换为字符串

时间:2010-08-20 13:46:31

标签: linq-to-entities concatenation aggregation

假设我有两个实体:

Physician
Credentials

医生可以拥有许多证书,例如琼斯博士可以拥有MD,DO,MPH作为证书。所以我需要通过Linq生成一个报告,将凭证连接成一个字符串。例如:

from p in Physicians
select
{
   p.Name
   p.Credentials (??? <- concatenated list of all credentials ?????)
}

我玩“p.Credentials.Aggregate((a,b) => a.Abrev + ',' + b.Abrev)”无济于事,但我不确定我的语法是否正确。

1 个答案:

答案 0 :(得分:0)

嗯......我没有测试过,但你可以尝试:

from p in Physicians
select
{
   p.Name,
   String.Join(",", p.Credentials.Select<Credentials, string>(c=>c.Abrev).ToArray())
}

进入Select,我认为x必须是Credential,而不是Credentials ......

修改

您需要将对象移动到内存中,尝试在进行选择

之前添加ToList()
from p in Physicians.Include("Credentials").ToList()
select new
{
    p.Name,
    Credentials = String.Join(",", p.Credentials.Select<Credentials, string>(c=>c.Abrev).ToArray())
}