I have following code and would like to translate it to linq. Is it possible, considering the two foreach loops inside the external foreach? Currently I could only translate the internal foreach loops to linq but still code is so long that I think that it might be shorter.
List<complexType> listOfComplex = ...some list...
List<complexType> newListOfCOmplex = new List<complexType>();
SomeObjectType someObject = ...some object...
foreach(var cT in listOfComplex)
{
var someObjectPropertyValue = someObject.property.FirstOrDefault(a=>a.value == smth);
if(someObjectPropertyValue == null)
{
return null;
}
var t = someObjectPropertyValue.Something.AnotherSomethin;
if(t==null)
{
newListOfCOmplex.Add(cT);
continue;
}
var collectionFirst = t.Where(s=>s.value == firstValue);
foreach (var f in collectionFirst)
{
someOtherMethod(f);
}
newListOfCOmplex.Add(cT);
var collectionSecond = t.Where(s=>s.value == secondValue);
foreach (var s in collectionSecond)
{
someOtherMethod(s);
}
}
Whole code is quite suspicious as variable
cTis only added to List, neither of it`s properties is checked inside foreach loop.Either this is original behaviour or consequence of obfuscation, you should revise your sample.
As for current sample better way to handle loop will be