linq to entities does not recognize the method int32 toint32

c# entity-framework int linq visual-studio-2010

Question

When I attempt to compare one int to another, I get this error (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 thoughts?

1
3
7/5/2012 3:26:57 PM

Accepted Answer

It seems thatCodePostal.Text is something that already exists in your context, therefore all you have to do is take that information out of the question.

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 unclear as to whereCommunesList comes from, however you could simply use: if it's empty before this.

CommunesList = db.Communes.Where(d => d.CodePostal == code).ToList();
8
7/5/2012 3:31:46 PM

Popular Answer

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



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow