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()逻辑来选择薪资值?
我试过了:
prevIoUsMonthPayroll.Where(t => t.EmployeeID == payroll.EmployeeID) .FirstOrDefault().Salary ?? default(decimal);
但是,如果prevIoUsMonthPayroll.Where(t => t.EmployeeID == payroll.EmployeeID).FirstOrDefault()为null,则抛出异常.
解决方法 正如所有答案所见,有几种方法可以做到这一点,我的首选方式是:var result = from payroll in currentMonthPayroll select new SalaryDifference { PrevIoUsMonthSalary = prevIoUsMonthPayroll .Where(t => t.EmployeeID == payroll.EmployeeID) .Select(prev => prev.Salary) .FirstOrDefault() };总结
以上是内存溢出为你收集整理的c# – Linq的三元 *** 作符全部内容,希望文章能够帮你解决c# – Linq的三元 *** 作符所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)