我有以下查詢:NHibernate的無法施展布爾到字符串
var query = from item in Session.Query<FactuurItem>()
where item.EnergieType == etype
&& (item.DienstType == null || item.DienstType == DienstType.Onbekend || item.DienstType == dtype)
&& item.IsActive == true
orderby item.Naam
select item;
將其轉化爲下面的SQL:
select * from [FactuurItem] factuurite0_
where
factuurite0_.EnergieType=?
and (factuurite0_.DienstType is null or factuurite0_.DienstType=? or factuurite0_.DienstType=?)
and case when factuurite0_.IsActive=1 then 'true' else 'false' end=case when ?='true' then 'true' else 'false' end
order by factuurite0_.Naam asc
這會導致異常:
{"Unable to cast object of type 'System.Boolean' to type 'System.String'."}
現在對於我的問題:爲什麼?
最初的查詢對我來說看起來不錯。但是,SQL不。這兩個案件陳述來自哪裏?顯然它會嘗試將屬性IsActive轉換爲SQL中的字符串,但它無法執行。
EDIT
好的,找到了解決辦法。映射等沒有錯,只是LINQ查詢被轉換爲SQL。特別是,如何這條線被翻譯:
&& item.IsActive == true
不知何故,這個被翻譯成複雜的CASE語句最終導致異常消息。但是,== true
-部分不是真的必要。通過刪除它,翻譯器不再會感到困惑並提供正確的SQL:
factuurite0_.IsActive=1
不再有CASE語句,也沒有更多的異常。
請向我們展示FactuurItem的映射。該映射定義了每個字段將映射到哪個SQL類型。 – 2011-05-26 12:50:49
既然您找到了解決方案,您可以通過在下面的框中提供答案來回答自己的問題。接下來,您可以通過單擊刻度圖標(但不是馬上)接受它。 – 2011-05-26 13:50:13