Reputation: 59
I have linq query with nullable datatime field which populate value based on condition.
var result=(from t1 in context.table1
join t2 in context.table2
on t1.id equals t2.fieldId
select new model1
{
name= t2.name,
DateCompleted = t1.Status == "Success"
? Convert.ToDateTime(t1.CompletedDate)
: Null
}).ToList();
Here DateCompleted can be nullable .If status is success then only I need Completed date.Other wise I need to show it null . Now the ": Null" portion is throwing error.
Thanks in advance Subin
Upvotes: 2
Views: 456
Reputation: 3154
Try the following code:
var result=(from t1 in context.table1
join t2 in context.table2
on t1.id equals t2.fieldId
select new model1
{
name= t2.name,
DateCompleted = t1.Status == "Success"
? Convert.ToDateTime(t1.CompletedDate)
: (DateTime?) null
}).ToList();
Upvotes: 3
Reputation: 3717
Try this
var result=(from t1 in context.table1
join t2 in context.table2
on t1.id equals t2.fieldId
select new model1
{
name= t2.name,
DateCompleted = t1.Status == "Success" ? Convert.ToDateTime(t1.CompletedDate): (DateTime?)null
}).ToList();
Upvotes: 5