以下是我的代码
var dbClaimLink = this.Context.Set<ClaimLink>();
var claims = await DbSet
.Include(claim => claim.Parent)
.Include(link => link.ParentLinks)
.ToListAsync();
var newClaimLink = await dbClaimLink.ToListAsync();
var processedClaims = claims.Select(x =>
{
var claimLinks = x.ParentLinks;
if (!claimLinks.Any())
{
return x;
}
var hiddenParents = claimLinks.Select(p => claims.Find(t => t.Id == p.ClaimLinkId));
x.HiddenParents = hiddenParents;
return x;
});
foreach (var objClaim in processedClaims)
{
if (objClaim.Children == null)
objClaim.Children = new List<Claim>();
var lst = newClaimLink.Where(k=> k.ClaimLinkId == objClaim.Id).ToList();
if (lst.Any())
{
foreach (var item in lst)
{
IEnumerable<Claim> newChildren = claims.Where(p => p.Id == item.ClaimId);
objClaim.Children.Concat(newChildren);
}
}
}
它总是返回没有与新孩子连接的旧孩子。我希望那些在foreach循环中连接的新旧子集
答案 0 :(得分:2)
Concat
方法返回包含两个值的新集合,但不会更改原始集合。
答案 1 :(得分:1)
Concat
将返回新对象 - 连接结果,因此您需要将其保存在某处:var result = objClaim.Children.Concat(newChildren);
Where
是延迟操作,只有在实现后(ToArray
或foreach
调用)才会执行,claims.Where(p => p.Id == item.ClaimId).ToArray()