假设我有两个实体:
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)
”无济于事,但我不确定我的语法是否正确。
答案 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())
}