I have to compare phone numbers in Linq to entities. I am using Regex so that I can compare only numbers while ignoring format.
As we can not use Regex in where clause so I get list of phone numbers and then applied loop to compare the phone numbers.
Here is the code:
    /// <summary>
    /// Check Phone number exist or Not
    /// </summary>
    /// <param name="accountNumber"></param>
    /// <returns></returns>
    public bool CheckPhoneNumberExist(string phoneNumber)
    {
        LP= new LPEntities();
        // In order to compare phone number, we need to use regex so that we can compare only numbers and not any specific format
        phoneNumber = Regex.Replace(phoneNumber, "[^0-9]", "");
        var phoneList = (from act in LP.Accounts
                         select act).ToList();
        if (phoneList.Count > 0)
        {
            foreach (var item in phoneList)
            {
                if (item.Telephone != null)
                {
                    if (Regex.Replace(item.Telephone, "[^0-9]", "") == phoneNumber)
                    {
                        return true;
                    }
                }
            }
        }
        return false;
      }
The code is working fine but there is a performance issue. We have 20 thousands records in the database which keep on increasing so its slow to make a list and then iterate through that list.
How might we optimize the above? Is there some other solution like if we can do it with stored procedure?
Thanks,
 
     
     
     
    