简体   繁体   中英

join returns 0 using DefaultIfEmpty()

I have Query contains more than one left inner join and returns List it join with table PayrollTransactions it returns o as it have no data wtuth this condention i need the list to be return in all cases even when the second join is empty

        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);
    }

i need to return List contains data as it returns 0 when the join with payrolltransation if its it contains o

it solved by moving The condition in second join to the select to be

       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,
             });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM