Null checking in LINQ Expression for SQL
automapper, c#, expression, linq, linq-to-entities
Solution
Simply use the `??` operator. It gets translated into COALESCE on the sql side
.ForMember(d => d.StartDate, opt => opt.MapFrom(s => (from foo in s.foo
orderby foo.ActualStartDate??foo.ProposedStartDate ascending
select foo.ActualStartDate??foo.ProposedStartDate).LastOrDefault()))
Problem
I have been tasked with creating LINQ expressions for Automapper, which must be able to be turned into an SQL expression. This means no method calls, which is where I am having problems. I have a record with two columns of interest - `ProposedStartDate` and `ActualStartDate`. I want to get the `ActualStartDate` if it exists, if null then get the `ProposedStartDate`. ``` .ForMember(d => d.StartDate, opt => opt.MapFrom(s => (from foo in s.foo orderby foo.ProposedStartDate ascending select foo.ProposedStartDate).LastOrDefault())) ``` So far I'm there, but unsure how I can check for nulls and pull the `ActualStartDate` in a safe way. Any help would be fantastic. Thanks.