我想模擬一個使用EntitySpaces的SQL代碼片段。我正要準備回退到好醇」原始SQL,但寧願學習如何正確地做到這一點...EntitySpaces/C#:如何在CASE語句中使用子查詢?
這是SQL我試圖重現:
SELECT
CASE WHEN GL.SOURCE = 'AP' THEN (SELECT COMPANY FROM VEND WHERE VEND.ID = GL.ID)
WHEN GL.SOURCE = 'AR' THEN (SELECT COMPANY FROM CUST WHERE CUST.ID = GL.ID)
WHEN GL.SOURCE = 'SB' THEN (SELECT COMPANY FROM SBMASTER WHERE SBMASTER.ID = GL.ID)
ELSE '' END AS COMPANY
FROM GL
LEFT OUTER JOIN ACCT ON GL.ACCT = ACCT.ACCT
我我一直在玩這個代碼,但沒有運氣。這是我的時刻了:
GlQuery qryGl = new GlQuery("qryGl");
AcctQuery qryAcct = new AcctQuery("qryAcct");
AcctQuery qryAcctSub = new AcctQuery("qryAcct");
VendQuery qryVendSub = new VendQuery("qryVend");
CustQuery qryCustSub = new CustQuery("qryCust");
SbmasterQuery qrySbmasterSub = new SbmasterQuery("qrySbmaster");
qryGl.Select
(
qryGl.Source.Case()
.When("AP").Then(qryVendSub.Select(qryVendSub.Company).Where(qryVendSub.Id == qryGl.Id))
.When("AR").Then(qryCustSub.Select(qryCustSub.Company).Where(qryCustSub.Id == qryGl.Id))
.When("SB").Then(qrySbmasterSub.Select(qrySbmasterSub.Company).Where(qrySbmasterSub.Id == qryGl.Id))
.Else("")
.End().As("COMPANY")
);
qryGl.LeftJoin(qryAcct).On(qryGl.Acct == qryAcct.Acct);
這給了我下面的(顯然是錯誤的!)輸出:
SELECT
[COMPANY] = CASE
WHEN 'AP' THEN MyProject.Com.Data.VendQuery
WHEN 'AR' THEN MyProject.Com.Data.CustQuery
WHEN 'SB' THEN MyProject.Com.Data.SbmasterQuery
ELSE ''
END
FROM [GL] qryGl
LEFT JOIN [ACCT] qryAcct ON qryGl.[ACCT] = qryAcct.[ACCT]
任何獲得該子查詢SQL到case語句幫助將不勝感激!
很新的EntitySpaces,所以希望它只是一些簡單的我已經錯過了......
乾杯
謝謝@JoelC。爲了讓它工作起來,我確實必須對SQL進行硬編碼(截止日期!)我想說我會按照您的建議來看看源代碼,但實際上我懷疑我會找時間! 是的,謝謝,我儘可能使用元數據。 – brad 2014-12-08 05:10:52