linq to entities does not recognize the method int32 toint32

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

Question

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 ?

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

Accepted Answer

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();
8
7/5/2012 3:31:46 PM

Popular Answer

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



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