Linq中的三元运算符


var result = from 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,
    };

是否有一种方法不重复previousMonthPayroll.Where(t => t.EmployeeId == payroll.EmployeeId).FirstOrDefault()逻辑来选择Salary值?

我试着:

previousMonthPayroll.Where(t => t.EmployeeId == payroll.EmployeeId)
    .FirstOrDefault().Salary ?? default(decimal);

但是,如果previousMonthPayroll.Where(t => t.EmployeeId == payroll.EmployeeId).FirstOrDefault()为null,则抛出异常

您可以使用Enumerable.DefaultIfEmpty

previousMonthPayroll.Where(t => t.EmployeeId == payroll.EmployeeId)
                    .DefaultIfEmpty(yourDefaultObject)
                    .First();

其中yourDefaultObject是一个默认工资为default(decimal)的对象

您可以使用let关键字:

from payroll in currentMonthPayroll
let prevSalary = previousMonthPayroll.FirstOrDefault(t => t.EmployeeId == payroll.EmployeeId)
select new SalaryDifference
{
    PreviousMonthSalary = prevSalary != null ? prevSalary.Salary : default(decimal)
};

查看MSDN

如果你使用的是c# 6.0,你可以将安全导航操作符与空合并操作符混合使用:

PreviousMonthSalary = previousMonthPayroll
                     .Where(t => t.EmployeeId == payroll.EmployeeId).FirstOrDefault()?.Salary 
                     ?? default(decimal)

从所有的答案来看,有几种方法可以做到这一点,我最喜欢的方法是:

var result = from payroll in currentMonthPayroll
             select new SalaryDifference
             {
                 PreviousMonthSalary = previousMonthPayroll
                     .Where(t => t.EmployeeId == payroll.EmployeeId)
                     .Select(prev => prev.Salary)
                     .FirstOrDefault()
             };

实际上,您正在做的是跨枚举序列的连接,所以为什么不这样说,让LINQ处理细节:

  var result = from currentPayroll in currentMonthPayroll
               join previousPayroll in previousMonthPayroll on currentPayroll.EmployeeId equals previousPayroll.EmployeeId
               into joined
               from r in joined.DefaultIfEmpty()
               select new SalaryDifference
               {
                 PreviousMonthSalary = r == null ? default(decimal) : r.Salary,
               };

相关内容

  • 没有找到相关文章

最新更新