在使用NHibernate级联全名的模糊搜索 [英] Fuzzy Search on a Concatenated Full Name using NHibernate

查看:206
本文介绍了在使用NHibernate级联全名的模糊搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下面的SQL转换成NHibernate的:

  SELECT * FROM dbo.Customer 
,其中姓+''+姓氏LIKE'%'+'鲍勃·史密斯'+'%'

我试着做这样的事情,但它不工作:

  NAME =%+姓名+%; 

VAR的客户= _session.QueryOver<客户>()
。凡(NHibernate.Criterion.Restrictions.On<客户>(C => c.FirstName +''+ C。姓氏)​​.IsLike(名))
的.List();



什么我基本上试图做的是能够在一个文本框来搜索客户名称与鲍勃·史密斯的例子价值和它使用上面的SQL的LIKE表达式搜索数据库。



如果我要去关于搜索的名字和。名字列错误,请帮我出一个替代,但上面的SQL查询获取我我需要什么。



更新有2个解决方案:



所以现在我已经发现了两个解决问题的对策。一种是使用了标准的API。以下岗位有伟大工程的答案: http://stackoverflow.com/a/2937100/670028



其他的解决方案,我发现多亏了我使用LINQ投影和匿名类型谁提出有益的同事之一。下面是使用LINQ的解决方案:

  VAR的客户= session.Query<客户>()
。选择(X = >新建{全名= x.FirstName ++ x.LastName,客户= X})
。凡(X => x.FullName.Contains(鲍勃·史密斯))
。选择(X => x.Customer)
.ToList();


解决方案

NHibernate的是不能将表达式转换成SQL语句,因为就是不知道做什么用C => c.FirstName +''+ c.LastName做。一个解决方案可以被改写这是这样的:

  Session.CreateCriteria<客户>()
。新增( Restrictions.Like(
Projections.SqlFunction(CONCAT,
NHibernateUtil.String,
Projections.Property(姓)
Projections.Constant(),
Projections.Property(名字)),
鲍勃Whiley,
MatchMode.Anywhere))


I am trying to convert the following SQL into NHibernate:

SELECT * FROM dbo.Customer
WHERE FirstName + ' ' + LastName LIKE '%' + 'bob smith' + '%'

I was trying to do something like this but it is not working:

name = "%" + name + "%";

var customers = _session.QueryOver<Customer>()
            .Where(NHibernate.Criterion.Restrictions.On<Customer>(c => c.FirstName + ' ' + c.LastName).IsLike(name))
            .List();

What I'm basically trying to do is be able to search for a customer's name in a text box with the example value of "bob smith" and for it to search the database using the LIKE expression in the SQL above.

If I'm going about searching the FirstName and LastName columns wrongly, please help me out with an alternative but the above SQL query gets me what I need.

Update with 2 solutions:

So I've now found two solutions to this problem. One is to use the Criteria API. The following post has an answer that works great: http://stackoverflow.com/a/2937100/670028

The other solution I found thanks to one of my helpful coworkers who suggested using a LINQ projection and anonymous types. Here's a solution using LINQ:

var customers = session.Query<Customer>()
    .Select( x => new { FullName = x.FirstName + " " + x.LastName, Customer = x } )
    .Where( x => x.FullName.Contains( "Bob Smith" ) )
    .Select( x => x.Customer )
    .ToList();

解决方案

NHibernate is not able to translate the expression into a sql statement because is does not know what to do with c => c.FirstName + ' ' + c.LastName. A solution can be rewriting this to something like this:

Session.CreateCriteria<Customer>()
    .Add(Restrictions.Like(
    Projections.SqlFunction("concat",
                            NHibernateUtil.String,
                            Projections.Property("FirstName"),
                            Projections.Constant(" "),
                            Projections.Property("LastName")),
    "Bob Whiley",
    MatchMode.Anywhere))

这篇关于在使用NHibernate级联全名的模糊搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆