2013-12-18 12 views
1

我想添加一個Restrictions.Eq與TSQL替換圍繞列名稱。我會怎麼做?在NHibernate中,如何將Replace添加到Restrictions.Eq?

string location = "Columbus OH"; 

var requestQuery = Session.CreateCriteria<Request>(); 
requestQuery.Add(Restrictions.Eq("Replace(LocationName,',','')", location); 
+0

您需要註冊功能'Replace'或使用SQL表達式,在這裏看到更多的細節http://stackoverflow.com/questions/1706259/nhibernate-filtering-by-user-定義函數 - 輸出 – Rippo

回答

1

一種可能的方法是與SqlProjection一起去。舉個例子:

string location = "Columbus OH"; 

var session = NHSession.GetCurrent(); 
var query = session.CreateCriteria<Request>(); 
query.Add(Restrictions 
    .Eq(// SQL Server function call 
     Projections.SqlProjection(
      "Replace(LocationName,',','') as Replacement" 
      , new[] {"Replacement"} 
      , new IType[] {NHibernateUtil.String}) 
     , location // searched value 
    )); 
var list = query.List<Request>();