join使用DefaultIfEmpty()返回0

问题描述 投票:-1回答:1

我有Query包含多个左内连接并返回List它连接表PayrollTransactions它返回o因为它没有数据wtuth这个压缩我需要列表在所有情况下都返回,即使第二个连接是空的

        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的连接返回0,如果它包含o

c# linq model-view-controller
1个回答
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,
             });
© www.soinside.com 2019 - 2024. All rights reserved.