我试图将以下嵌套的foreach循环转换为linq查询
foreach (SAP.MOD mod in mods){
foreach (SAP.RefMod r in refs){
if (mod.RefModId == r.RefModId){
modLookup.Add(mod.Modcode, r.ModItem);
break;
}
}
}
答案 0 :(得分:0)
尝试使用join
:
var matches = mods.Join(refs,
m => wo.RefModId,
r => p.RefModId,
(mod,ref) => new {mod.RefModId, mod.Modcode, ref.ModItem}
);
matches.All(x => modLookup.Add(x.Modcode, x.ModItem));
答案 1 :(得分:0)
var query = from m in mods
join r in refs on m.RefModIf equals r.RefModId
select new {
mod.Modcode,
r.ModItem,
};
foreach (var i in query)
modLookup.Add(i.Modcode, i.ModItem);
...如果modLookup是一个字典,你可以这样做,而不是foreach
...
modLookup = query.ToDictionary(i=>i.Modcode, i=>i.ModItem);