c# - Ternary operator in Linq -
var result = payroll in currentmonthpayroll select new salarydifference { previousmonthsalary = previousmonthpayroll.where(t => t.employeeid == payroll.employeeid).firstordefault() == null ? default(decimal) : previousmonthpayroll.where(t => t.employeeid == payroll.employeeid).firstordefault().salary, };
is there way not repeat previousmonthpayroll.where(t => t.employeeid == payroll.employeeid).firstordefault()
logic pick salary
value?
i tried:
previousmonthpayroll.where(t => t.employeeid == payroll.employeeid) .firstordefault().salary ?? default(decimal);
but, if previousmonthpayroll.where(t => t.employeeid == payroll.employeeid).firstordefault()
null, throws exception.
as witnessed of answers, there several ways of doing this, preferred way be:
var result = payroll in currentmonthpayroll select new salarydifference { previousmonthsalary = previousmonthpayroll .where(t => t.employeeid == payroll.employeeid) .select(prev => prev.salary) .firstordefault() };
Comments
Post a Comment