查詢我有一個相對簡單的F#
查詢表達式聯接:leftOuterJoin和`.DefaultIfEmpty()`在F#
let mdrQuery =
query {
for header in db.CustomerDetails do
leftOuterJoin row in db.MDR_0916
on (header.PID = row.PID) into result
select (result, header)
}
這將返回每個header
和result
但對於一個header
,至今尚未row
匹配, result
只是一個空序列,當查詢結果被傳遞給一個自定義類型時,我得到一個錯誤,說明與row
中的字段關聯的構造函數未定義。這對於在row
中沒有匹配的任何header
是有意義的,則返回null
序列。舉個例子:
mdrQuery |> Seq.head;;
val it :
seq<dbSchema.ServiceTypes.MDR_0916> * dbSchema.ServiceTypes.CustomerDetails
= (seq [null], CustomerDetails {ACCOUNTMANAGER = null;
ACCOUNTSTATUS = "XC";
ADDRESSLINE1 = null;
ADDRESSLINE2 = null;
ADDRESSLINE3 = null;
ADDRESSLINE4 = "123 PIG ROAD"...
我懷疑有周圍the leftOuterJoin
documentation here這一點,因爲一種方式。但是,當我嘗試使用這個例子作爲我查詢模板:
let mdrQuery =
query {
for header in db.CustomerDetails do
leftOuterJoin row in db.MDR_0916
on (header.PID = row.PID) into result
for row in result.DefaultIfEmpty() do
select (result, header)
}
的.DefaultIfEmpty()
件錯誤出與
error FS0039: The field, constructor or member 'DefaultIfEmpty' is not defined
有沒有辦法,我可以使這個連接發生和選擇每行,填充result
中的不匹配行與None
(或其他一些空的SQL空值),以便可以將整個查詢傳遞給我的記錄類型?
在理想情況下,輸出爲不匹配的行會是這樣的(由下面的手產生截短的結果)
mdrQuery |> Seq.head;;
val it :
seq<dbSchema.ServiceTypes.MDR_0916> * dbSchema.ServiceTypes.CustomerDetails
= (MDR_0916 {AIMExp = null;
AP = null;
APComp = null;
APEng = null;
APFine = null;
APForl = null;...},
CustomerDetails {ACCOUNTMANAGER = null;
ACCOUNTSTATUS = "XC";
ADDRESSLINE1 = null;
ADDRESSLINE2 = null;
ADDRESSLINE3 = null;
ADDRESSLINE4 = "123 PIG ROAD"...
編輯:This question/answer類似於礦,但包括ToOption result
簡單地輸出一個Some (seq [null])
。
在您的交互式輸出中,'result'不是一個空序列,而是一個元素的序列,而那個元素是'null'。 –
感謝您的澄清。 – Steven
'DefaultIfEmpty'是一個擴展方法,所以你需要'打開System.Linq'。 – kvb