Well, you can use the extension method, which gets the accessor delegate and only executes it if the element is not null :
public static TResult ConditionalAccess<TItem, TResult>(this TItem item, Func<TItem, TResult> accessor) where TResult : Class { if (item == null) { return null; } else { return accessor(item); } }
You can use it, for example, as follows:
NomPointDeVente = joined.VisitePdvProduit.ConditionalAccess(_ => _.PointDeVente.NomPointDeVente);
You can easily create versions of this method for operations that do not return a value (i.e. bar.ConditionalAccess(_ => _.Foo()) ) or return value types.
source share