简体   繁体   English

Asp.Net MVC3,Bool Column null检查

[英]Asp.Net MVC3, Bool Column null check

public Nullable<bool> BROUGHT { get; set; } // EDMX generate this code, so I can not change this

I want to null check for the BROUHGT (DB) column. 我想要检查BROUHGT(DB)列。

so I write code like 所以我写代码就像

if (table.BROUGHT != DBNull.Value && Convert.ToBoolean(table.BROUGHT)){..}

but the error message say, 但错误信息说,

Error   2   Operator '!=' cannot be applied to operands of type 'bool?' and 'System.DBNull' ...

How I null check for that column? 我如何null检查该列?

Thank you! 谢谢!

Entity Framework is an ORM, and it's shiedling you from having to think about DBNull.Value . 实体框架是一个ORM,它让你不必考虑DBNull.Value So just check against null like you would in other C# code: 所以只需像在其他C#代码中那样检查null

if (table.BROUGHT != null && table.BROUGHT.Value){..}

Not that since it's a nullable type, to get the actual bool value you have to either use the .Value property (as above) or cast it to a bool . 不是因为它是可以为空的类型,要获得实际的bool值,您必须使用.Value属性(如上所述)或将其bool转换为bool

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM