C# – Linq – check condition in where clause if field can be null

clinq

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

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; 

Best Answer

can you do

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