linq.Any() check on large database Is there a way to speed up the Linq.Any check or perform the check in another way to enhance performance.  

I have a mastercodes table that records unique codes for a campaign.  The code below will create new codes for a campaign but checks the mastercode table to make sure that it doesn;t already exist. Once the codes have been created and added to the mastercodes table it creates an excel file for download.

The main issue is that a campaign can have 400k codes and we have several campaigns running at once. So the mastercode table can get into the millions of records. When I request 300 new codes and run the below process without the .Any() check it takes 1-2 seconds in production. However when i add the check the process times out with the "no response to user" timeout error.   

I'm using the Z.EntityFrameworrk.Extensions and the post to the table happens pretty much instantly.  But is there a way to code the perform the mastercode check that doesn't have a huge performance issue. 

The Model

public class MasterCode
{
    public int MasterCodeId { get; set; }
    public int CampaignId { get; set; }
    public string CampaignCode { get; set; }
    public string Code { get; set; }
    public bool Used { get; set; }
    public DateTime SubmittedOn { get; set; }
}


The code

public static void GenerateMasterCodes(GenerateMasterCode model)
{
    using (ApplicationDbContext _context = new ApplicationDbContext())
    {
        Campaign campaign = _context.Campaigns.Find(model.CampaignId);

        List&lt;MasterCode&gt; masterCodes = new List&lt;MasterCode&gt;();
        for (var i = 1; i &lt;= model.Count; i++)
        {
            var masterCode = new MasterCode
            {
                CampaignId = campaign.CampaignId,
                CampaignCode = campaign.CampaignCode,
                Used = false,
                SubmittedOn = DateTime.Now
            };

            // Ensure that no code already exists for that campaign.
            var code = GenerateMasterCode(model.StartString, model.CodeLength);
            while (_context.MasterCodes.Any(m =&gt; m.Code == code &amp;&amp; m.CampaignCode == campaign.CampaignCode))
            {
                code = GenerateMasterCode(model.StartString, model.CodeLength);
            }
            masterCode.Code = code;

            masterCodes.Add(masterCode);
        }

        try
        {
            _context.MasterCodes.AddRange(masterCodes);
            _context.BulkSaveChanges();
        }
        catch (Exception ex)
        {
            ...
        }
    }
}


The Code generator

public static string GenerateMasterCode(string startString, int mastercodeLength)
{
    string numericChars = "X123456789";

    char[] chars = new char[mastercodeLength];
    Random rd = new Random();


    for (int i = 0; i &lt; mastercodeLength; i++)
    {
        if (!string.IsNullOrEmpty(startString) &amp;&amp; i &lt; startString.Length)
        {
            chars[i] = Char.Parse(startString.Substring(i, 1));
        }
        else
        {
            chars[i] = numericChars[rd.Next(0, numericChars.Length)];
        }
    }

    return new string(chars);
}
GenerateMasterCode
I would break the GenerateMasterCode() method into two methods and would declare the Random as a static field tof the class.
From the Random documentation:

The default seed value is derived from the system clock and has finite resolution. As a result, different Random objects that are created in close succession by a call to the default constructor will have identical default seed values and, therefore, will produce identical sets of random numbers. This problem can be avoided by using a single Random object to generate all random numbers.

Instead of using a char[] to generate the random characters I would use a StringBuilder.
public static string GenerateMasterCode(string startString, int mastercodeLength)
{

    if (startString.Length == masterCodeLength) { return startString; }
    if (startString.Length &gt; masterCodeLength) { return startString.Substring(0, masterCodeLength); }

    return startString + GenerateRandomCharacters(masterCodeLength - startString.Length);
}

private static Random rd = new Random();
private static string GenerateRandomCharacters(int length)
{
    string numericChars = &quot;X123456789&quot;;
    int length = numericChars.Length;
    StringBuilder builder = new StringBuilder(length);
    for (int i=0; i &lt; length; i++)
    {
        builder.Append(numericChars[rd.Next(length)]);
    }
    return builder.ToString();
}


GenerateMasterCodes
Right now you query in the while loop all of the MasterCodes although you are only interested in the Campain related MasterCodes. So you should query the MasterCodes which belongs to the Campain before you start with the for loop.
Assigning false to Used can be omitted, because that is the default value of bool.
You can use the var type for the List&lt;MasterCode&gt; as well.
If you don't use that method outside of the class you should consider to make it private.
public static void GenerateMasterCodes(GenerateMasterCode model)
{
    using (ApplicationDbContext _context = new ApplicationDbContext())
    {
        Campaign campaign = _context.Campaigns.Find(model.CampaignId);
        var campainRelatedMasterCodes = _context.MasterCodes.Where(m.CampaignCode == campaign.CampaignCode);
        var masterCodes = new List&lt;MasterCode&gt;();
        for (var i = 1; i &lt;= model.Count; i++)
        {
            var masterCode = new MasterCode
            {
                CampaignId = campaign.CampaignId,
                CampaignCode = campaign.CampaignCode,
                SubmittedOn = DateTime.Now
            };

            // Ensure that no code already exists for that campaign.
            var code = GenerateMasterCode(model.StartString, model.CodeLength);

            while (campainRelatedMasterCodes.Any(m =&gt; m.Code == code))
            {
                code = GenerateMasterCode(model.StartString, model.CodeLength);
            }
            masterCode.Code = code;

            masterCodes.Add(masterCode);
        }

        try
        {
            _context.MasterCodes.AddRange(masterCodes);
            _context.BulkSaveChanges();
        }
        catch (Exception ex)
        {
            ...
        }
    }
}