2016-10-11 34 views
-2

我想尽量将它转换为lambda,但无法成功。有没有什么方法可以转换为linq或lambda?嵌套的foreach循环转换为lambda或linq

foreach (var tempitem in mbsRateTempList) 
{ 
    foreach (var Saveditem in mbsSavedRecordList) 
    { 
     if (tempitem.MbsSecurityId == Saveditem.MbsSecurityId && tempitem.CouponRate == Saveditem.CouponRate 
       && tempitem.SettlementMonth.Month == Saveditem.SettlementMonth.Month && tempitem.Price == Saveditem.Price) 
     { 
      TobeDeletedIds.Add(Saveditem.Id); 
      MatchedIdsInTempList.Add(tempitem.TempId); 
      //mbsSavedRecordList[0].ObjectState=Repository.Pattern.Base.Infrastructure.ObjectState. 
     } 
     //else 
     //{ 

     //} 
    } 
} 
+0

欢迎堆栈溢出。请举例说明您正在使用的数据,您拥有的代码实体以及您得到的错误。编辑你的问题是可以的。 –

回答

0

您可以Join在包含所有相关属性,匿名类型:

var objectsToAdd = from tempitem in mbsRateTempList 
        join saveditem mbsSavedRecordList 
        on new { tempitem.MbsSecurityId, tempitem.CouponRate, tempitem.SettlementMonth.Month, tempitem.Price } 
        equals new { saveditem.MbsSecurityId, saveditem.CouponRate, saveditem.SettlementMonth.Month, saveditem.Price } 
        select new { tempitem, saveditem }; 

foreach(var x in objectsToAdd) 
{ 
    TobeDeletedIds.Add(x.saveditem.Id); 
    MatchedIdsInTempList.Add(x.tempitem.TempId); 
}