linq to entities无法识别int32 toint32的方法

Was*_*RAR 3 c# linq int entity-framework visual-studio-2010

当我尝试将int与int进行比较时(当比较字符串工作时)我收到此错误

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);
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

有任何想法吗 ?

Jon*_*eet 8

它看起来像是CodePostal.Text在你现有的上下文中 - 所以你要做的就是从查询中提取它:

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);
Run Code Online (Sandbox Code Playgroud)

目前尚不清楚CommunesList从哪里来 - 但如果在此之前它是空的,你可以使用:

CommunesList = db.Communes.Where(d => d.CodePostal == code).ToList();
Run Code Online (Sandbox Code Playgroud)

  • 我非常爱你 (3认同)