我尝试使用DefaultIsEmpty进行左外部连接:
// Get Raw Queryables
var mainQuery = context.Query<OrderBudgetOverviewBudgetListModel>().AsQueryable();
var openOrdersQuery = context.Query<OrderBudgetOverviewOpenOrderListModel>().AsQueryable();
var purchasedOrdersQuery = context.Query<OrderBudgetOverviewPurchasedOrderListModel>().AsQueryable();
// Filtering
if (year.HasValue)
{
mainQuery = mainQuery.Where(x => x.BudgetYear == year.Value);
openOrdersQuery = openOrdersQuery.Where(x => x.OrderDateYear == year.Value);
purchasedOrdersQuery = purchasedOrdersQuery.Where(x => x.OrderDateYear == year.Value);
}
if (team.HasValue)
{
mainQuery = mainQuery.Where(x => x.Team == team.Value);
openOrdersQuery = openOrdersQuery.Where(x => x.Team == team.Value);
purchasedOrdersQuery = purchasedOrdersQuery.Where(x => x.Team == team.Value);
}
// Grouping
var orderOpenGroupedData = openOrdersQuery
.GroupBy(x => new { x.ObjectAccountNumber, Year = x.OrderDateYear, x.Team })
.Select(x => new { x.Key.ObjectAccountNumber, x.Key.Year, x.Key.Team, TotalCost = x.Sum(z => z.TotalCost) });
var orderPurchedGroupedData = purchasedOrdersQuery
.GroupBy(x => new { x.ObjectAccountNumber, Year = x.OrderDateYear, x.Team })
.Select(x => new { x.Key.ObjectAccountNumber, x.Key.Year, x.Key.Team, TotalCost = x.Sum(z => z.InvoiceAmount) });
// Fill together
var resultList = (from main in mainQuery
join open in orderOpenGroupedData on new { main.Team, main.ObjectAccountNumber, main.BudgetYear } equals new { open.Team, open.ObjectAccountNumber, BudgetYear = open.Year }
into openData
from open in openData.DefaultIfEmpty()
join purchased in orderPurchedGroupedData on new { main.Team, main.ObjectAccountNumber, main.BudgetYear } equals new { purchased.Team, purchased.ObjectAccountNumber, BudgetYear = purchased.Year }
into purchasedData
from purchased in purchasedData.DefaultIfEmpty()
select new { main }).ToList();
我知道“ open”和“ Purchased”可以为空-因此我会对其进行检查,但是基本上我在这里不使用它,而我仍然得到NullRefExc。
这是SQL事件探查器中的查询:
exec sp_executesql N'SELECT [x].[BudgetAmount], [x].[BudgetDescription], [x].[BudgetYear], [x].[ObjectAccountNumber], [x].[Team], [t].[ObjectAccountNumber], [t].[Year], [t].[Team], [t].[TotalCost], [t0].[ObjectAccountNumber], [t0].[Year], [t0].[Team], [t0].[TotalCost]
FROM [_INTRANET_OrderBudgetOverview_BudgetList] AS [x]
LEFT JOIN (
SELECT [x0].[ObjectAccountNumber], [x0].[OrderDateYear] AS [Year], [x0].[Team], SUM([x0].[TotalCost]) AS [TotalCost]
FROM [_INTRANET_OrderBudgetOverview_OpenOrderList] AS [x0]
WHERE [x0].[OrderDateYear] = @__year_Value_1
GROUP BY [x0].[ObjectAccountNumber], [x0].[OrderDateYear], [x0].[Team]
) AS [t] ON (([x].[Team] = [t].[Team]) AND ([x].[ObjectAccountNumber] = [t].[ObjectAccountNumber])) AND ([x].[BudgetYear] = [t].[Year])
LEFT JOIN (
SELECT [x1].[ObjectAccountNumber], [x1].[OrderDateYear] AS [Year], [x1].[Team], SUM([x1].[InvoiceAmount]) AS [TotalCost]
FROM [_INTRANET_OrderBudgetOverview_PurchasedOrderList] AS [x1]
WHERE [x1].[OrderDateYear] = @__year_Value_2
GROUP BY [x1].[ObjectAccountNumber], [x1].[OrderDateYear], [x1].[Team]
) AS [t0] ON (([x].[Team] = [t0].[Team]) AND ([x].[ObjectAccountNumber] = [t0].[ObjectAccountNumber])) AND ([x].[BudgetYear] = [t0].[Year])
WHERE [x].[BudgetYear] = @__year_Value_0',N'@__year_Value_1 int,@__year_Value_2 int,@__year_Value_0 int',@__year_Value_1=2017,@__year_Value_2=2017,@__year_Value_0=2017
这是结果: