2012-07-05 99 views
3

,当我尝试比较INT为int我得到这个错误(比较它的工作原理字符串时)LINQ到实体无法识别方法INT32 toint32

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

任何想法?

+1

在您的lINQ调用之前将CodePostal.Text转换为整数。 – 2012-07-05 15:32:54

回答

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

目前尚不清楚其中CommunesList来自 - 但如果它之前是空的这一点,你可以只使用:

CommunesList = db.Communes.Where(d => d.CodePostal == code).ToList(); 
+3

我很爱你 – 2012-07-05 15:36:17

+0

刚刚遇到同样的问题,这个解决方案帮助。这是因为linq在执行查询之前通过不转换解析表达式的方式? – 2013-03-14 04:21:46

+0

@AndreDublin:是的 - 最初查询包含解析部分,而我将它分隔开以便它包含值。 – 2013-03-14 06:45:30

0

使用此: db.Communes.Where(d => d.CodePostal ==(INT)CodePostal.Text)

相关问题