联接使用DefaultIfEmpty()返回0

时间:2019-03-24 08:11:33

标签: c# linq model-view-controller

我有查询包含多个左内部联接并返回列表 它与表PayrollTransactions联接,它返回o,因为它没有数据,即使在第二次联接为空的情况下,在所有情况下我都需要返回列表

        public List<PayrollElementsViewModel> GetAllPayrollRunDetails(int? PayrollrollRunID)
    {
          IQueryable<PayrollElementsViewModel> List =
                (from R in database.PayrollElements
                 where R.Deleted == false
                 && R.PayrollElementsPayrollRunID == PayrollrollRunID

                 join Emp in database.Employee on R.PayrollElementsIDEmployeeID equals Emp.EmployeeID
                 into g
                 from Emp in g.DefaultIfEmpty()

                 join tran in database.PayrollTransactions on Emp.EmployeeID equals tran.PayrollTransactionsEmployeeID
                 into g6
                 from tran in g6.DefaultIfEmpty()
                 where tran.PayrollTransactionsPayrollRunID == PayrollrollRunID

                 select new PayrollElementsViewModel
                 {
                     PayrollElementsPayrollRunID = PayrollrollRunID,
                     PayrollElementsEmployeeID = Emp.EmployeeID,
                     PayrollElementsEmployeeName = Emp.EmployeeName,
                     PayrollElementsEmployeeFingerPrint = Emp.EmployeeFingerPrint,
                     PayrollElementsStartDate = R.PayrollElementsStartDate,
                     PayrollElementsEndDate = R.PayrollElementsEndDate,
                     PayrollElemenTsransactionsValue = tran.PayrollTransactionsValue
                 });

            var results = List.ToList();
            return (results);
    }

我需要返回List包含的数据,因为与payrolltransation进行连接时,如果它包含o,则它返回0

1 个答案:

答案 0 :(得分:0)

通过将第二个联接中的条件移动到要选择的

来解决
       join tran in database.PayrollTransactions on Emp.EmployeeID equals tran.PayrollTransactionsEmployeeID
             into g6
             from tran in g6.DefaultIfEmpty()

             select new PayrollElementsViewModel
             {
                 PayrollElemenTsransactionsValue = tran.PayrollTransactionsPayrollRunID == PayrollrollRunID?tran.PayrollTransactionsValue  : 0,
             });
相关问题