你需要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
@邁克爾陣列 - 檢查我的更新您的錯誤。 –