你需要group by
,你可以得到的IGrouping
适用String.Join
: -
var result = (from inspArch in inspectionArchives
from inspAuth in inspArch.InspectionAuthority
group new { inspArch, inspAuth } by inspArch.CustomerId into g
select new
{
Id = String.Join(",",g.Select(x => x.inspArch.Id),
clientId = x.Key,
authId = String.Join(",",g.Select(x => x.inspAuth.Id)
}).ToList();
这里最棘手的部分是组中的两个对象即new { inspArch, inspAuth }
因为我们需要从两个访问性能。
更新:
由于这是实体框架,它将不能够在方法String.Join
转换为SQL,所以我们可以使用AsEnumerable
带回分组的对象到内存中,然后投射像这样: -
var result = (from inspArch in inspectionArchives
from inspAuth in inspArch.InspectionAuthority
group new { inspArch, inspAuth } by inspArch.CustomerId into g
select g).AsEnumerable()
.Select(g => new
{
Id = String.Join(",",g.Select(x => x.inspArch.Id),
clientId = x.Key,
authId = String.Join(",",g.Select(x => x.inspAuth.Id)
}).ToList();
使用的客户端ID –
组@ M.kazemAkhgary我tryed,但我需要创建leftand右侧 – Michael
@迈克尔阵列 - 检查我的更新您的错误。 –