2012-11-02 79 views
0
List<tblX> messages = (from x in db.tblX where x.msg_id == id_id 
          || x.name == firstName select x).ToList(); 

的非空值我得到的错误:'tblX'上的属性'x'不能设置为'null'值。您必须将此属性设置为类型“的Int16”

The property 'x' on 'tblX' could not be set to a 'null' value. You must set this property to a non-null value of type 'Int16'.

我在DB msg_blocked一个属性,它可为空和整数。我知道我需要进行转换,但是我并没有在linq中使用它或需要它。

+0

向您展示tblX类。 –

+0

@Asif tblX位于.edmx文件中。 –

+0

@Damith你认真吗? –

回答

3

好像你的类定义tblX不匹配数据库表示,所以要么修改你的类接受一个空值,或者只是伸出必填字段:

List<tblX> messages = (from x in db.tblX 
        where (x.msg_id == id_id || x.name == firstName) 
        select new tblX 
        { 
        //required fields 
        msg_id = x.msg_id, 
        name = x.name, 
        ... 
        }).ToList(); 

附录:你遇到这个问题的原因是幕后,当你 select x 这是翻译成 select new tblX 投影到所有可用的字段。提供的代码更加明确,并指定要查询哪些字段然后投影到哪些字段。

1
List<tblX> messages = (from x in db.tblX 
         where (x.msg_id == id_id || x.name == firstName) && 
           x.msg_blocked != null 
         select x).ToList(); 
相关问题