Performing multiple operations if condition is satisfied in ternary operator in linq

Posted by user1575914 on Stack Overflow See other posts from Stack Overflow or by user1575914
Published on 2012-09-06T09:37:03Z Indexed on 2012/09/06 9:38 UTC
Read the original article Hit count: 172

Filed under:

I am a beginner in LINQ.I want to perform some conditional operation lik follows,

(from emp in Employees 
let DOB=emp.BirthDate.GetValueOrDefault()
let year=DOB.Year
let month=DOB.Month
let EmpAgeInYearsToday=DateTime.Now.Year-year
let EmpAgeInMonthToday=DateTime.Now.Month-month
let temp_year=(EmpAgeInYearsToday-1)
let ExactNoOfMonths_temp=EmpAgeInMonthToday<0?temp_year:EmpAgeInMonthToday
let ExactNoOfMonths=EmpAgeInMonthToday<0?EmpAgeInMonthToday+12&temp_year:EmpAgeInMonthToday
select new{emp.EmployeeID,DOB,
EmployeeAgeToday=EmpAgeInYearsToday+" Years "+ExactNoOfMonths+" Months ").Dump();

Here,

let ExactNoOfMonths=EmpAgeInMonthToday<0?EmpAgeInMonthToday+12&temp_year:EmpAgeInMonthToday

This part is not working. How to achieve this? How to perform multiple operations when the condition is satisfied?

© Stack Overflow or respective owner

Related posts about LINQ