Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linq to entities does not recognize the method int32 toint32

I get this error when I try to compare int to int (when comparing string it works)

IEnumerable<Commune> myCommunes = from d in db.Communes
                                  where d.CodePostal == Convert.ToInt32(CodePostal.Text)
                                  select d;

foreach (Commune c in myCommunes)
{
    CommunesList.Add(c);
}

enter image description here

Any ideas ?

like image 932
Wassim AZIRAR Avatar asked Dec 10 '25 09:12

Wassim AZIRAR


1 Answers

It looks like CodePostal.Text is something within your existing context - so all you've got to do is extract that from the query:

int code = Convert.ToInt32(CodePostal.Text); // Or use int.Parse...

// Not using a query expression here as it just adds extra cruft
IEnumerable<Commune> myCommunes = db.Communes.Where(d => d.CodePostal == code);

It's not clear where CommunesList comes from - but if it's empty before this, you could just use:

CommunesList = db.Communes.Where(d => d.CodePostal == code).ToList();
like image 88
Jon Skeet Avatar answered Dec 12 '25 23:12

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!