Farmer
Farmer

Reputation: 10983

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 ?

Upvotes: 3

Views: 13604

Answers (2)

muthuvel
muthuvel

Reputation: 1132

Use this: db.Communes.Where(d => d.CodePostal == (int)CodePostal.Text)

Upvotes: -1

Jon Skeet
Jon Skeet

Reputation: 1500485

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();

Upvotes: 8

Related Questions