具有多个左外连接的内联接
本文关键字:连接 | 更新日期: 2023-09-27 18:36:41
我有以下语句,我正在尝试将其转换为 LINQ to SQL:
FROM mc.WORKORDER AS wo WITH (NOLOCK)
INNER JOIN mc.WORKTASK AS wt WITH (NOLOCK)
ON wo.wordoi = wt.wrkord_oi
AND wo.tmplt = 0
AND wo.ci_cnc_date IS NULL
AND wt.ci_cnc_date IS NULL
AND wt.clsdt_date >= CAST('1/1/' + CAST(year - 1 AS varchar) AS DateTime)
AND wt.clsdt_date < DATEADD("mm", 1, CAST(CAST(quarter * 3 AS varchar) + '/1/' + CAST(year AS varchar) AS DateTime))
LEFT OUTER JOIN mc.WOLABOR AS wol WITH (NOLOCK)
ON wt.wtskoi = wol.wt_oi
LEFT OUTER JOIN mc.CREW AS c WITH (NOLOCK)
ON wt.crew_oi = c.crewoi
LEFT OUTER JOIN mc.WORKORDERTYPE AS wot WITH (NOLOCK)
ON wt.wot_oi = wot.wotyoi
LEFT OUTER JOIN mc.SEGMENTEDACCOUNT AS sa WITH (NOLOCK)
ON wt.segacctref_oi = sa.sgaccoi
LEFT OUTER JOIN mc.SEGVALUE AS sv WITH (NOLOCK)
ON sa.segval4ref_oi = sv.sgvaloi
LEFT OUTER JOIN mc.PRIORITY AS p WITH (NOLOCK)
ON wo.prio_oi = p.priooi
LEFT OUTER JOIN mc.SITE AS s WITH (NOLOCK)
ON wo.BEparn_oi = s.siteoi
所以我是这样开始翻译的:
int year = 2012, quarter = 1;
string monthYear = "1/1";
DateTime closeDate = new DateTime(monthYear, (year-1));
DateTime timeframe = new DateTime(year, (quarter * 3), 01).AddMonths(1);
var allWorkorders =
(from wo in WORKORDERs
join wt in WORKTASKs on wo.Wordoi equals wt.Wrkord_oi && wo.Tmplt equals 0 && wo.Ci_cnc_date == null && wt.ci_cnc_date == null &&
wt.clsdt_date >= (string)closeDate && wt.clsdt_date < timeframe
现在从这里我试图直接转到第一个左外连接。 但是,编译器告诉我,在时间框架结束时需要一个分号。
我是否仍可以在 var allWorkOrders 中完成其他联接,还是必须创建一个新联接?
您遇到的第一个问题是如何连接表。 您需要几个where
子句和不同的join
. 此外,对于查询的其余部分,它应遵循类似于以下内容的模式:
var allWorkorders = from wo in WORKORDERs
where wo.Tmplt == 0 && wo.Ci_cnc_date == null
join wt in WORKTASKs
on wo.Wordoi equals wt.Wrkord_oi
where wt.ci_cnc_date == null
&& wt.clsdt_date >= (string)closeDate
&& wt.clsdt_date < timeframe
join wol in WOLABOR
on wt.wtskoi equals wol.wt_oi into WOLABORGroup
join c in CREW
on wt.crew_oi equals c.crewoi into CREWGroup
// more outer joins here
//....
from WOLABORItem in WOLABORGroup.DefaultIfEmpty()
from CREWItem in CREWGroup.DefaultIfEmtpy()
// more from clauses here
//...
select new
{
Something = WOLABORItem != null ? WOLBORItem.something : "none",
// more items
//.....
};