Convert Nested Foreach to Linq

前端 未结 3 1967
情书的邮戳
情书的邮戳 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条回答
  •  旧时难觅i
    2021-01-20 02:14

    Why Linq? it's unlikely to be any faster than what you've got. It's unlikely to be any simpler or easier to read or easier to debug than what you've got.

    If you're looking to improve performance, Your nested loops run on O(mn) time, where m is the size of the assemblies collection and n is the size of the aggregates collection, so basically O(n2) time.

    Are the collections orderd by the key values? If so, merging the two collections by iterating over the two in lockstep will be a big win. That should bring you down to something like O(n) performance.

    If they are not ordered, you can get a win by taking the aggregates collection and converting it into a lookup table like a Dictionary, prior to iterating over the assemblies collection. Then its a straight iteration over assemblies with a presumably fast lookup against the dictionary.

提交回复
热议问题