我有以下代码,想把它翻译成 linq。考虑到外部 foreach 中的两个 foreach 循环,是否有可能?目前我只能将内部 foreach 循环转换为 linq,但代码仍然很长,我认为它可能会更短。
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);
}
}