mardi 16 janvier 2018

Short "if" statement without "else"

I have a class:

public class DriverShortInfoElement
{
    ......
    public DateTime? VistracksDateAdded { get; set; }
}

and I want to fill it by data from DbContext. I want to fill VistracksDateAdded only some condition is true, otherwise leave as null:

I try such way:

        var list = (from i in _db.Drivers
                    select new DriverShortInfoElement()
                    {
                        .....
                        VistracksDateAdded = (i.ProblemSyncToVistracksDriver != null) ?? i.ProblemSyncToVistracksDriver.DateAdded
                    }).ToList();

and such way:

        var list = (from i in _db.Drivers
                    select new DriverShortInfoElement()
                    {
                        .....
                        VistracksDateAdded = (i.ProblemSyncToVistracksDriver != null) ? i.ProblemSyncToVistracksDriver.DateAdded : null
                    }).ToList();

both are compilation error. How to do it this simple thing?

Aucun commentaire:

Enregistrer un commentaire