拥有一个返回包含searchstring的所有值的搜索功能

时间:2014-05-22 15:30:42

标签: c# nhibernate asp.net-mvc-5 queryover

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

我目前的搜索功能:

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

最终查询会有一些限制,但首先我需要过滤数据,以便列表只包含包含searchString的值。 我目前正在.Where(x => x.Value.Contains(searchString))部分获得System.Exception。 有人有什么想法吗?

提前致谢!

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

改变这个:

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

有了这个:

.WhereRestrictionOn(x => x.Value).IsLike(searchString, MatchMode.Anywhere)

答案 2 :(得分:-1)

x.Value可能是NULL的问题?

.WHERE(x.Value != null && x.Value.Contains(searchString))
相关问题