//Get linked claim for children list from ClaimLink
foreach (var claim in processedClaims)
{
if (claim.Children == null)
{
claim.Children = new List<Claim>();
}
var claimRelationList = newClaimLink.Where(k=> k.ClaimLinkId == claim.Id).ToList();
if (claimRelationList.Any())
{
//Get the claim for all selected ClaimLink
foreach (var claimLink in claimRelationList)
{
var newChildren = claims.Where(p => p.Id == claimLink.ClaimId).ToList();
claim.Children = claim.Children != null && claim.Children.Any() ? newChildren.Concat(claim.Children) : newChildren;
}
}
}
I want to reduce the execution time in above two foreach loop. Is there have better way than this?
You can remove the if (claimRelationList.Any()) condition as the following foreach will not iterate over an empty list.
You already know that claim.Children is not null within that for each because you initialized it above. So I would replace
claim.Children = claim.Children != null && claim.Children.Any() ? newChildren.Concat(claim.Children) : newChildren;
With
claim.Children.AddRange (newChildren).
Multiple options i see at first sight:
ToList(), not necessary here unless you have some sort of query executing in the background, see comment of @Shelby115newClaimLink.Where(k=> k.ClaimLinkId == claim.Id) could be replaced by a dictionary created beforehand like newClaimLink.ToDictionary(k=> k.ClaimLinkId)claims.Where(p => p.Id == claimLink.ClaimId)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With