![](/img/trans.png)
[英]The X property on Y could not be set to a 'Decimal' value. You must set this property to a non-null value of type 'Single'
[英]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'
List<tblX> messages = (from x in db.tblX where x.msg_id == id_id
|| x.name == firstName select x).ToList();
我得到错误:
无法将“ tblX”上的属性“ x”设置为“空”值。 您必须将此属性设置为'Int16'类型的非空值。
我在db中有一个msg_blocked属性,该属性可以为null和整数。 我知道我需要进行转换,但是我不使用它,也不需要在linq中的任何地方使用它。
似乎您的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
,并select new tblX
到其所有可用字段中。 提供的代码更加明确,并指定了要查询然后投影到的字段。
List<tblX> messages = (from x in db.tblX
where (x.msg_id == id_id || x.name == firstName) &&
x.msg_blocked != null
select x).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.