Linq-检查where子句中的条件是否字段可以为null [英] Linq - check condition in where clause if field can be null

查看:592
本文介绍了Linq-检查where子句中的条件是否字段可以为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题-即使项目没有参考,如何检查where子句中的条件?

I have question - how to check condition in where clause even when item has no reference?

最基本的方法-我正在检查类中的字段,该字段可以为null.当我以这种方式检查它时,它将返回Null Reference Exception

Most basic way - I am checking field from my Class, which can be null. When I just check it in that way it will return Null Reference Exception

 var soldOutProducts = from p in list 
            where p.destinataire.StartsWith("D") 
            select p; 

推荐答案

你能做

var soldOutProducts = from p in list
                      where !string.IsNullOrEmpty(p.destinataire) and
                            p.destinataire.StartsWith("D")
                      select p;

这篇关于Linq-检查where子句中的条件是否字段可以为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆