I want to convert a nested foreach loop in C# to Linq. I know how to do it if the two lists are of same type. But in my case, they are of different type, except that they both have two common properties, DocumentId and IsValid. I have to loop through the outer list and the inner list and whenever the DocumentId of both are the same, then assign the IsValid value from the outer loop object to the inner loop object. I have the following foreach loop is working fine.
foreach (var docs in outerDocuments)
{
foreach (var presentedDocuments in innerdocuments)
{
if (docs.DocumentId.Equals(presentedDocuments.DocumentId))
{
presentedDocuments.IsValid = docs.IsValid;
}
}
}
I need to convert it to Linq. Usually ReSharper does a good job of refactoring such code. But in this case it converted only the nested foreach to Linq, and even then it threw a warning , "Access to foreach variable in closure".
Thanks
ForEachanyway. That's probably part of why ReSharper didn't clean it up