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);
}
Any ideas ?
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();