tags:

views:

986

answers:

2

I'm trying to produce an IF conditional statement inside of a "select new" that checks the values of two fields in order to fill a property.

from e in Employees
where e.EmployeeID == id
select new {
    EmployeeID = e.EmployeeID,
    EmployeeName = e.FirstName + " " + e.LastName,
    Status = (if e.col1.HasValue then "This Value" else if e.col2.HasValue then "Other Value")
}

The columns are nullable and therefore the column types are DateTime? data types.

Only one or the other column will have a datetime value, not both.

How do I go about doing this???

+4  A: 
Status = e.col1.HasValue ? "This Value" : (e.col2.HasValue ? "Other Value" : null)
Mehrdad Afshari
Perfect. Thanks.
kntcnrg
A: 

use the null coelacing operator for this

Status = e.col1.HasValue ? e.col1.Value ?? "This Value" : e.col2.Value ?? "Other Value"
Rony
Actually it does the reverse of what the OP specifies.
Mehrdad Afshari
yes you are right
Rony