2012-07-26 61 views
0

我有以下方法:IQUERY沒有運行預期

ServiceType GetWithValidServices(long ServiceTypeID); 

及以下功能:

public ServiceType GetWithValidServices(long ServiceTypeID) 
    { 
     IQuery query = CurrentSession.CreateQuery("select dl.ServiceType from Service as dl" 
     + "where dl.ServiceType.ID = :id and dl.IsValid = true" 
     + "order by dl.HavePriority desc"); 

     query.SetInt64("id", ServiceTypeID); 

     var dlArt = query.UniqueResult<ServiceType>(); 
     return dlArt; 
    } 

在下面的方法我稱之爲上面提到的功能:

public ServiceCollection GetService(long ServiceTypeID) 
    { 

     ServiceType d = DomainToWebgridMapper.GetWebgridServiceType(DaoFactory.Instance.ServiceTypeDao.GetWithValidService(ServiceTypeID)); 

     return d.Service; 
    } 

我的問題是查詢運行不正確。我可以看到服務,但dl.IsValid沒有運行的過濾器,也沒有按照優先級排序。

我在少數其他方法中使用where子句,並且它工作正常。

我不知道這裏出了什麼問題。也許有人可以幫助我。

在此先感謝

回答

0

我想我明白你的問題是什麼;它與你如何連接字符串來創建查詢有關。考慮這個了片刻:

string query = "select dl.ServiceType from Service as dl" 
    + "where dl.ServiceType.ID = :id and dl.IsValid = true" 
    + "order by dl.HavePriority desc"; 

因爲你不插入在開始任何空格或換行/你的字符串,端部你的查詢變成是這樣的:

select dl.ServiceType from Service as dlwhere dl.ServiceType.ID = :id and dl.IsValid = trueorder by dl.HavePriority desc 
             ^^^^^^^           ^^^^^^^^^ 

看到我標記了問題?爲了解決這個問題,除了最後一個連接起來組成查詢的字符串之外,在所有字符的末尾添加一個額外的空格,或者改爲使用類似verbatim string literal的東西。

IQuery query = CurrentSession.CreateQuery(
    "select dl.ServiceType from Service as dl " + 
    "where dl.ServiceType.ID = :id and dl.IsValid = true " + 
    "order by dl.HavePriority desc" 
); 
+0

謝謝,我已經試過了,但查詢仍然沒有正確運行 – Paks 2012-07-26 10:51:47

相關問題