简体   繁体   English

NHibernate.Linq LIKE

[英]NHibernate.Linq LIKE

How can I produce this query using NHibernate.Linq? 如何使用NHibernate.Linq生成此查询?

WHERE this_.Name LIKE @p0; @p0 = 'test'  // Notice NO % wild card

Note, this is not Linq To Sql or Entity Framework. 注意,这不是Linq To Sql或Entity Framework。 This is NHibernate. 这是NHibernate。

Edit: 编辑:

Here is the desired query using ICriteria: 以下是使用ICriteria的所需查询:

criteria.Add(Expression.Like("Name", "test"));
return criteria.List<Theater>();

Whilst this has been marked as resolved, which was correct at the time, may I also note that NHibernate has some extensions now so you can do the following: 虽然这已被标记为已解决,但当时是正确的,我可能还注意到NHibernate现在有一些扩展,所以你可以做以下事情:

Session.QueryOver<MyEntity>()
    .Where(x => x.Property.IsLike("something", MatchMode.Anywhere))
    .List();

This will do a LIKE '%something%' for you. 这将为你做一个LIKE '%something%'

I believe this is what you are looking for: 我相信这就是你要找的东西:

var theaters = from theater in Session.Linq<Theater>() 
               where theater.Name.Contains("test") 
               select theater;

According to my tests it generates an SQL 'LIKE' statement: "... WHERE theater.Name LIKE %test%" 根据我的测试,它生成一个SQL'LIKE'语句:“... WHERE theater.Name LIKE%test%”

which is exactly the output of the criteria snippet you have provided. 这正是您提供的标准片段的输出。

I had the same problem in my project and found a solution : 我在项目中遇到了同样的问题并找到了解决方案:

session.Linq<Theater>()
    .Where(x => x.Name.StartsWith("test") && x.Name.EndsWith("test");

This translates in SQL to 这在SQL中转换为

SELECT ... WHERE Name LIKE '%test' AND Name LIKE 'test%'

With NH 4 (and probably a bit earlier), a built-in Like string extension is available within NHibernate.Linq namespace: Like(this string matchExpression, string sqlLikePattern) . 使用NH 4(可能稍早一点), NHibernate.Linq命名空间中可以使用内置的Like字符串扩展: Like(this string matchExpression, string sqlLikePattern) (It is defined on NHibernate.Linq.SqlMethods extension class.) (它在NHibernate.Linq.SqlMethods扩展类上定义。)

using NHibernate.Linq;
...
session.Query<Theater>()
    .Where(t => t.Name.Like("test"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM