2014-05-22 233 views
0

我试图让我的搜索整个数据库中列“价值”为包含搜索字符串的所有结果我的网页上的搜索功能的所有值的搜索功能。我为我的项目使用NHibernate,我仍然习惯于应该指定查询的方式。有返回包含搜索字符串

我目前的搜索功能:

[HttpGet] 
public ActionResult Search(string searchString) 
{ 
    var searchResult = DbSession.QueryOver<Translation>()               
           .Where(x => x.Value.Contains(searchString)) 
           .List(); 
    return Json(searchResult, JsonRequestBehavior.AllowGet); 
} 

最终的查询将有更多的限制,但首先我需要对数据进行筛选,使得列表仅包含包含搜索字符串的值。 目前,我在.Where(x => x.Value.Contains(searchString))部件上收到System.Exception。 任何人有任何想法?

在此先感谢!

+1

什么是确切的例外呢? System.Exception真的是异常代码抛出吗? – qxg

回答

1

QueryOver是LINQ不同,所以你必须使用WhereRestrictionOn方法。或者使用符合条件的Where。下面是一个例子使用WhereRestrictionOn

[HttpGet] 
public ActionResult Search(string searchString) 
{ 
    var searchResult = DbSession.QueryOver<Translation>() 
          .WhereRestrictionOn(x => x.Value).IsLike("%" + searchString + "%") 
          .List(); 
    return Json(searchResult, JsonRequestBehavior.AllowGet); 
} 

查看更多在这里:http://nhibernate.info/blog/2009/12/17/queryover-in-nh-3-0.html

+0

谢谢!这就像一个魅力:) – Guinn

-1

可能与x.Value是NULL的问题吗?

.WHERE(x.Value != null && x.Value.Contains(searchString)) 
+1

请注意,他正在使用'QueryOver'不链接。它不支持'string.Contains'如下所示:http://nhforge.org/blogs/nhibernate/archive/2009/12/17/queryover-in-nh-3-0.aspx。 – LawfulHacker

1

更改此:

.Where(x => x.Value.Contains(searchString)) 

有了这个:

.WhereRestrictionOn(x => x.Value).IsLike(searchString, MatchMode.Anywhere) 
相关问题