Updating items of one List that match another List I'm using VS 2008. I pass a List&lt;T&gt; a using ref, along with another List&lt;T&gt; b.

I need to cycle thru List&lt;T&gt; b, which contains around 200,000 items, and for each item in b, it it matches criteria in a, which contains 100,000 or more items, and update it.

public static void UpdateList(ref List&lt;ExampleT&gt; a, List&lt;ExampleT&gt; b)
{

    var count = 0;
    for (int i = 0; i &lt; b.Count; i++)
    {

        foreach (var z in a.FindAll(x =&gt; x.MatchA == b[i].MatchA &amp;&amp; x.MatchB == b[i].MatchB))
        {
            z.ToUpdate = b[i].ToUpdate;
            count++;
        }
        if(count &gt;= 10000)
            break;
    }
}


Now, the only reason I added count was so that once 10000 items in a have been updated, the loop will stop, otherwise it will take a very long time to go thru all of the items. Even by limiting it like this, it still takes around 3 min. 

For the sake of the example, T in this case is an object similar to the following:

public class ExampleT
{
public string MatchA {get;set;}
public string MatchB {get;set;}
public double? ToUpdate {get;set;} //in List a this will ALWAYS be null until it gets updated in the foreach
}


Is there a way I can make this more efficient?
Your code is O(n m), where n is the length of a and m is the length of b.

You could make it O(n + m) (assuming only a small number of items from a match each item in b) by using a hash table. One possible way to do this is to use ToLookup():

var aLookup = a.ToLookup(x =&gt; new { x.MatchA, x.MatchB });

foreach (var bItem in b)
{
    foreach (var aItem in aLookup[new { bItem.MatchA, bItem.MatchB }])
        aItem.ToUpdate = bItem.ToUpdate;
}


Another way to do basically the same thing would be using join:

var pairs = from bItem in b
            join aItem in a
              on new { bItem.MatchA, bItem.MatchB } equals new { aItem.MatchA, aItem.MatchB }
            select new { bItem, aItem };

foreach (var pair in pairs)
    pair.aItem.ToUpdate = pair.bItem.ToUpdate;


Also, you should really use better variable names.