Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
对我来说检查空值的最佳方法是什么,以便在执行此语句时不会出现错误:
if (Levels.Count(x => x.Location.ToUpper() == code.ToUpper()) == 1)
我需要确保 Location 不为空,因为它不断抛出对象引用异常。
试试这个
if (Levels.Count(x => x.Location!= null && x.Location.ToUpper() == code.ToUpper()) == 1)
你可以试试Where运营商
Where
if (Levels.Where(x => x.Location != null) .Count(x => x.Location.ToUpper() == code.ToUpper()) == 1)