C# join使用DefaultIfEmpty()返回0
我有一个查询包含多个左内部联接和返回列表 它与表PayrollTransactions连接,它返回o,因为它没有数据。通过此压缩,我需要在所有情况下都返回列表,即使第二个连接为空C# join使用DefaultIfEmpty()返回0,c#,linq,model-view-controller,C#,Linq,Model View Controller,我有一个查询包含多个左内部联接和返回列表 它与表PayrollTransactions连接,它返回o,因为它没有数据。通过此压缩,我需要在所有情况下都返回列表,即使第二个连接为空 public List<PayrollElementsViewModel> GetAllPayrollRunDetails(int? PayrollrollRunID) { IQueryable<PayrollElementsViewModel> Li
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);
}
public List GetAllPayrollRunDetails(int?PayrollRunId)
{
可查询列表=
(来自database.PayrollElements中的R)
其中R.Deleted==false
&&R.PayrollElementsPayrollRunID==PayrollRunId
在数据库中加入Emp。R.PayrollElementSideEmployeeId上的员工等于Emp.EmployeeID
进入g
来自g.DefaultIfEmpty()中的Emp
在Emp.EmployeeID上的database.PayrollTransactions中加入tran等于tran.payrolltransactionemployeeid
加入g6
从g6.DefaultIfEmpty()中的tran
其中tran.PayrollTransactionsPayrollRunID==PayrollRunId
选择new PayrollElementsViewModel
{
PayrollElementsPayrollRunID=PayrollRunId,
PayrollElementsEmployeeID=Emp.EmployeeID,
PayrollElementsEmployeeName=Emp.EmployeeName,
PayrollElementsEmployeeFingerPrint=Emp.EmployeeFingerPrint,
PayrollElementsStartDate=R.PayrollElementsStartDate,
PayrollElementsEndDate=R.PayrollElementsEndDate,
PayrollElementsTransactionsValue=tran.PayrollTransactionsValue
});
var results=List.ToList();
返回(结果);
}
我需要返回包含数据的列表,因为当使用PayrollTransaction的联接时,它返回0,如果它包含o它通过将第二个联接中的条件移动到要选择的对象来解决
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,
});
请使查询尽可能简单,以显示您的问题。请选中“编辑”复选框您得到了什么错误?第二次联接中的条件返回0,查询应大于0