2010-08-05 30 views
2

所以這裏的使用無關位代碼冷落:使用NHibernate.Linq並獲取2個查詢進行簡單選擇,爲什麼?

public IEnumerable<T> GetByQuery(Expression<Func<T, bool>> filter 
{ 
    try 
    { 
     return Session.Linq<T>().Where(filter); 
    } 
    catch(Exception ex) 
    { 
     // custom exception handling here 
    } 
    finally 
    { 
     CloseSession(); 
    } 
    return null; 
} 

和它的一個例子被稱爲這個樣子的:

IEnumerabl<ClientReport> clientReports = 
clientReportRepository.GetByQuery(item => item.ClientId = id); 

因此,大家可以看到,沒有什麼花哨和被稱爲在這我們在數據庫中打了一張桌子,與其他桌子沒有任何關係。但是,當我在配置中使用show_sql = true時,它顯示2個相同的查詢。 任何想法? 謝謝

+0

您確定兩個查詢實際上是使用NHProf執行的嗎? – DanP 2010-08-05 18:16:59

+0

我讓dba監視數據庫並執行兩個查詢。當我調用GetByQuery方法時,似乎有人運行,然後當我列舉迭代下面列舉的查詢時再次運行。 – Sean 2010-08-07 18:04:47

回答

2

clientReports可能會執行查詢,每次你枚舉它(或得到Count(),例如)。

爲避免這種情況,請在作業中使用.ToList()

相關問題