Linq-Iffieldisnull,checkconditioninwhereclause我有疑惑-即使item在子句中没有引用条件,如何检查where?最基本的方法-我正在检查我班级中的一个字段,该字段可以为空。当我以这种方式检查它时,它返回空引用异常varsoldOutProducts=frompinlistwherep.destinataire.StartsWith("D")selectp;你可以做varsoldOutProducts=frompinlistwhere!string.IsNullOrEmpty(p.destinataire)andp.destinataire.StartsWith("D")selectp;首先检查null,就像您在循环中编写普通C#代码一样。wherep.destinataire!=null&&p.destinataire.StartsWith("D")如果p本身可以为null(即你的列表可以包含null元素),那么你还需要检查它:wherep!=null&&p。destinataire!=null&&p.destinataire.StartsWith("D")请注意,如果您的查询表达式只是过滤,您可能需要使用点表示法:varsoldOutProducts=list.Where(p=>p.destinataire!=null&&p.destinataire.StartsWith("D"));当查询变得复杂时,查询表达式很有用——尤其是在连接和分组时。以上就是C#学习教程:Linq——如果字段为null,检查where子句中的条件,分享所有内容,如果对大家有用还需要详细了解C#学习教程,希望大家多多指教多注意它—varsoldOutProducts=frompinlistwherep.destinataire!=null&&p.destinataire.StartsWith("D")selectp;如需转载请注明出处:
