C# Double Foreach Optimization
<p>I got a problem over here, recently I found this problem with a web project (created in Sitefinity) when I'm trying to get a whole list of articles that I need to add in cache for faster access.</p>

<p>I found that some genius developers XD used to get the whole list of articles that belong to certain category that exists in the Taxon (some property of sitefinity that recognizes that type of category ex: Pregnant - Babies - kids...you got the idea), and have to do some search in an item collection with dynamic content, where has the info about the articles (title, images, styles, texts).</p>

<p>Well, my problem is they did it with a double loop research using ForEach, and my boss wants to "optimize" that loop, cause, well of course, is taking too long to do that loop (sometimes it can be more than 3 or 4 minutes, and yeah, my boss wants to have that process instantly :) ).</p>

<p>here is the code:</p>

<pre><code>   foreach (HierarchicalTaxon subt in listWeek)
   {
       foreach (DynamicContent item in myCollection)
       {
            if (item.Organizer.TaxonExists("Category", subt.Id))
            {
                 item.Author = subt.Name;
                 //if (articles.Where(art =&gt; art.Author.Equals(item.Author)).Count() == 0)
                 articles.Add(item);
            }
       }
   }
</code></pre>

<p>So I have to deal with this double loop that has this 2^n implementation, my question is, how can I do it?!?!?!</p>

<p>I was thinking in adding some LINQ as I saw in other threads, but it seems to be way too slower than this actual code.</p>

<p>It looks that myCollection doesn't have a long set of elements, but I'm pretty sure I'll have to use this kind of optimization in other sentences with the same logic in other projects.</p>

<p>please guys, a little back up would be apreciated, is there a possible to do it faster??</p>

<hr>

<p>So here we go, what do you think guys, is this ok??</p>

<pre><code>var data = listWeek.ToDictionary(x =&gt; x.Id, x =&gt; x.Name);
var iDdata = data.Keys;
var nombresData = data.Values;

foreach (DynamicContent item in myCollection)
{
    if (item.Organizer.TaxonExists("Category", iDdata.First()) )
    {
        item.Author = nombresData.First();
        articlesPrueba1.Add(item);
    }
}
</code></pre>

<p>Edit: 16/08/2016 --> Not currently working, First of all, I got troubles when I'm trying to access the values of the dictionary, so I attach in separate variables for Id and Name, I think that should do it, but I'm not sure about that.</p>

<p>But then I had this problem, is just doing one round in the content of the dictionary data, it seems obvious, but I thought that'd work, but sadly, it didn't.</p>

<hr>

<p>I was trying to use LINQ:</p>

<pre><code> foreach (HierarchicalTaxon subt in listWeek)
 {
    foreach (DynamicContent item in myCollection.Where(item =&gt; item.Organizer.TaxonExists("Category", subt.Id)))
    {
        item.Author = subt.Name;
        articlesPrueba2.Add(item);
        }
    }
 }
</code></pre>

<p>but I got this exception about the query is not accessing the database, damn you Sitefinity, I Hate You... But nevermind, I don't really care about LINQ.</p>

<p>still in serious optimization problem.</p>