2011-03-21 61 views
2

我正在使用NHibernate驅動存儲庫,Fluent映射並嘗試使用Linq to NHibernateC#NHibernate簡單問題

但對於這樣

Retrieve<XValue>(x => (x.Timestamp.CompareTo(start) >= 0 && 
         x.Timestamp.CompareTo(end) <= 0)); 

// 'Retrieve' here acts simply as 'session.Query<T>().Where(expression);' 

我得到以下結果一些簡單的查詢:

System.NotSupportedException: Int32 CompareTo(System.DateTime) 

我不知道爲什麼,但CompareTo操作不投射到數據庫和輸出也有點奇怪:

create table "QuotUnitDescriptor" (
    Id integer, 
    PaperId INTEGER, 
    Timestamp DATETIME, 
    InPaperIdx INTEGER, 
    primary key (Id) 
) 

NHibernate: INSERT INTO "QuotUnitDescriptor" ...................... 

// Many INSERT's 

NHibernate: select cast(count(*) as INTEGER) as col_0_0_ 
    from "QuotUnitDescriptor" binaryunit0_ 

我不明白爲什麼這個操作會調用select -> integer操作。

應該如何實現以下面向日期的查詢?(使用Linq更好,但標準也很好,我認爲)。

+0

你在比較你的時間戳爲0.也許這是爲什麼? – Vadim 2011-03-21 20:55:41

+0

@Yads我將它與我的代碼中的'start'和'end'時間戳比較。 – 2011-03-21 20:58:28

回答

5

NHibernate.Linq提供程序無法將CompareTo調用轉換爲sql。

使用類似:

Retrieve<XValue>(x => x.Timestamp>start && x.Timestamp<end); 

附:並避免存儲庫。這是一個天真的抽象。

+0

謝謝。您能否評論您關於存儲庫的聲明?我應該用什麼樣的抽象來代替它們?那些更具領域特色的? – 2011-03-21 21:02:46

+1

@ Yippie-Kai-Yay如果你使用的是NHibernate,不要試圖將它抽象出來。確保你想和自己綁在一起,一旦你做到了 - 活着就是了。看起來很好(使用存儲庫)直到你開始面對更深的對象圖和性能問題。確保你有更豐富的領域模型來驗證狀態變化,而不是嘗試抽象你應用程序的無聊'read'方面。 http://ayende.com/Blog/archive/2011/03/16/architecting-in-the-pit-of-doom-the-evils-of-the.aspx – 2011-03-21 21:08:09