【发布时间】:2011-10-20 10:36:56
【问题描述】:
我正在尝试将复杂(而且相当老套)的动态 SQL 查询转换为 LINQ 查询。
到目前为止,我有以下 LINQ 查询:
var results = (
from c in Customers
from d in MonthCalendar
join f in Facilities on c.CustomerCode equals f.CustomerCode
join p in ProjectedCashFlows on f.FacilityId equals p.FacilityId into pj
from p in pj.DefaultIfEmpty()
where d.ExpectedYear == currentYear
&& f.FacilityStatusId == 1
&& (p.ExpectedYear == null || d.ExpectedYear == p.ExpectedYear)
&& (p.ExpectedMonth == null || d.ExpectedMonth == p.ExpectedMonth)
&& c.PrimaryArmId == userId
&& (p.ProjectedCashFlowStatusId == null || p.ProjectedCashFlowStatusId != 4)
select new
{
CustomerCode = c.CustomerCode,
CustomerName = c.CustomerName,
FacilityId = f.FacilityId,
FacilityDescription = f.FacilityProductDescription,
FacilityCurrency = f.FacilityCurrencyId,
FacilityLimit = f.Limit,
ExpectedYear = d.ExpectedYear,
ExpectedMonth = d.ExpectedMonth,
ExpectedAmount = p == null ? 0 : (double)p.ExpectedAmount
}
);
我正在尝试从与Facilities 表具有一对多关系的Customer 表中检索详细信息。然后我试图检索位于ProjectedCashFlows
我遇到的问题是,无论ProjectedCashFlows 表中是否存在任何值,查询都应返回所有Customer 和Facilites 信息。
不幸的是,该查询没有这样做 - 它仅在 ProjectedCashFlows 表中存在设施时返回 Customer 和 Facilities 信息。
我使用MonthCalender 表列出了一年中的每个月。
相关表信息为:
客户
- 客户代码
- 客户名称
- PrimaryArmId
设施
- 客户代码
- 设施 ID
- FacilityCurrencyId
- 设施限制
- 设施描述
预计现金流量
- 客户代码
- 设施 ID
- 预计年份
- 预计月份
- 预期金额
- ProjectedCashFlowStatusId
月历
- 预计月份
- 预计年份
例如,我有一个客户在 Facilities 表中有 4 行,但是其中 2 个设施没有出现在 ProjectedCashFlows 表中,因此它们没有被显示。
如果ProjectedCashFlows 中不存在条目,则应从CalendarMonths 表中获取ExpectedMonth 和ExpectedYear,为ExpectedAmount 返回0 并使用Facilities 表中的FacilityId。
你可能知道我刚刚开始使用 LINQ。
任何人都可以戳我正确的方向吗?
【问题讨论】:
标签: c# linq linq-to-sql .net-3.5