I'm trying to convert the below nested foreach loops to a linq query
foreach (SAP.MOD mod in mods){
foreach (SAP.RefMod r in refs){
if (mod.RefModId == r.RefModId){
modLookup.Add(mod.Modcode, r.ModItem);
break;
}
}
}
Try using 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));
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);
... if modLookup is a dictionary you could do this instead of the foreach
...
modLookup = query.ToDictionary(i=>i.Modcode, i=>i.ModItem);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.