Convert Nested Foreach to Linq

前端 未结 3 2030
情书的邮戳
情书的邮戳 2021-01-20 01:39

I am trying to re-write the following using LINQ

foreach (WMCommon.Services.StakeOut.assembly ass in assemblies) 
{
  foreach (var agg in aggregate) 
  {
           


        
3条回答
  •  长发绾君心
    2021-01-20 02:03

    I am hoping that the LINQ will be much faster than nested FOREACH?

    In general, LINQ isn't going to improve your performance unless you change how it works. LINQ is effectively just performing the iterations for you.

    In this case, it appears you could just use a join to improve this overall, as this is going to give you the same effect:

    var query = from WMCommon.Services.StakeOut.assembly ass in assemblies
                join agg in aggregate
                on new { ass.unitActn, ass.unitCode, ass.unitLength } equals new { (agg.catagory.unitActn, agg.catagory.unitCode, agg.catagory.unitLength }
                select new { ass, agg };
    
    foreach(var pair in query)
        pair.ass.quantity = pair.agg.qty;
    

提交回复
热议问题