LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression

m0n5t3r picture m0n5t3r · Jul 1, 2013 · Viewed 11.9k times · Source

I get this error when I try this code :

TaxiEntities db = new TaxiEntities();
bool IsUserPassCorrected = db.tblOperators.Any(item => item.UserName.ToLower() == txtUserName.Text.ToLower() &&
item.Password == Convert.ToInt32(txtPassWord.Text));

if (!IsUserPassCorrected)
{
    MessageBox.Show("Username or Password is incorrected! Please try again");
}

Answer

cuongle picture cuongle · Jul 1, 2013

Since LINQ to Entities does not support Convert.ToInt32, you need to parse to int outside LINQ first:

TaxiEntities db = new TaxiEntities();
int password = int.Parse(txtPassWord.Text);

bool IsUserPassCorrected = db.tblOperators
            .Any(item => item.UserName.ToLower() == txtUserName.Text.ToLower() 
                      && item.Password == password);