简体   繁体   English

如何处理空值?

[英]How to handle null value?

I am writing one LINQ to select data from entity. 我正在编写一个LINQ来从实体中选择数据。 I have list of data with different attributes. 我有不同属性的数据列表。 One of the attribute could be null. 其中一个属性可能为null。 so when it get null it giving me an below error 所以当它变为null时它会给我一个以下错误

Error: 错误:

Object reference not set to instance of an object

Here is LINQ code: 这是LINQ代码:

var maxName = NameLookup.Select(c => c.DESC.Length).Max()

If DESC has null value than it should be also acceptable. 如果DESC具有空值,那么它也应该是可接受的。 I mean to say i want to handle if it contains null value. 我的意思是说我想处理它是否包含空值。

尝试这个:

var maxName = NameLookup.Select(c => (c.DESC != null) ? c.DESC.Length : 0).Max()
var  maxName = NameLookup.Max(c => c.DESC!= null ? c.DESC.Length : 0);

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

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