How to check NULL in LINQ queries? RRS feed

  • Question

  • I have long Linq query which involves several entity objects. In the query below any of the entity object could be null. Whats the prefered way to check null

    // i know i can do something like below but is there any other prefered way
    if(account != null &&
    account.Property != null &&
    account.Property.Client != null &&
    account.Property.Client.Contracts.SingleOrDefault(x=>x.Type == "Default") != null)
     bool flag = account.Property.Client.Contracts.SingleOrDefault(x=>x.Type == "Default").Flag;

    Monday, August 20, 2012 3:18 PM