嵌套的 foreach 循环转换为 lambda 或 linq
Nested foreach loop conversion to lambda or linq
我正在努力将其转换为 lambda 但未能成功。有什么方法可以转换成linq或lambda吗?
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
//{
//}
}
}
您可以 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);
}
我正在努力将其转换为 lambda 但未能成功。有什么方法可以转换成linq或lambda吗?
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
//{
//}
}
}
您可以 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);
}