Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to Imitate C# 6 Null-Conditional operator in C# 5

I have a situation where I need to assign some objects' properties inside an object initializer. Some of these objects can be null and I need to access their properties, the problem is that they are too many, and using a if/else thing is not good.

Example

visits = visitJoins.AsEnumerable().Select(joined => new VisitPDV()
{
    VisiteId = joined.Visite.VisiteId.ToString(),
    NomPointDeVente = joined.VisitePdvProduit.PointDeVente.NomPointDeVente,             
});

The joined.VisitePdvProduit can be null, and the problem is that there are like dozens of such assignments (I just took one to shorten the code)

The C# 6 Null-Conditional operator is the perfect solution for this situation, the problem is that I'm on C# 5 in this project, is there a way to imitate that ?

like image 648
AymenDaoudi Avatar asked Dec 01 '15 18:12

AymenDaoudi


2 Answers

Well, you can use an extension method that receives an accessor delegate and only executes it if the item isn't 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 like this:

NomPointDeVente = joined.VisitePdvProduit.ConditionalAccess(_ => _.PointDeVente.NomPointDeVente);

You can easily create versions of this method for operations that don't return a value (i.e. bar.ConditionalAccess(_ => _.Foo())) or return value types.

like image 91
i3arnon Avatar answered Nov 06 '22 20:11

i3arnon


Like this. Ugly, but what had to be done.

 visits = visitJoins.AsEnumerable().Select(joined => new VisitPDV()
 {
     VisiteId = joined.Visite.VisiteId.ToString(),
     NomPointDeVente = (joined.VisitePdvProduit == null) ? null : joined.VisitePdvProduit.PointDeVente.NomPointDeVente,             
 });
like image 21
Nick Whaley Avatar answered Nov 06 '22 20:11

Nick Whaley