mxm*_*ile 22 linq nhibernate linq-to-nhibernate
如何使用NHibernate.Linq生成此查询?
WHERE this_.Name LIKE @p0; @p0 = 'test' // Notice NO % wild card
Run Code Online (Sandbox Code Playgroud)
注意,这不是Linq To Sql或Entity Framework.这是NHibernate.
编辑:
以下是使用ICriteria的所需查询:
criteria.Add(Expression.Like("Name", "test"));
return criteria.List<Theater>();
Run Code Online (Sandbox Code Playgroud)
Kez*_*zer 24
虽然这已被标记为已解决,但当时是正确的,我可能还注意到NHibernate现在有一些扩展,所以你可以做以下事情:
Session.QueryOver<MyEntity>()
.Where(x => x.Property.IsLike("something", MatchMode.Anywhere))
.List();
Run Code Online (Sandbox Code Playgroud)
这将为你做一个LIKE '%something%'
.
tol*_*sm7 17
我相信这就是你要找的东西:
var theaters = from theater in Session.Linq<Theater>()
where theater.Name.Contains("test")
select theater;
Run Code Online (Sandbox Code Playgroud)
根据我的测试,它生成一个SQL'LIKE'语句:"... WHERE theater.Name LIKE%test%"
这正是您提供的标准片段的输出.
CMe*_*rat 16
我在项目中遇到了同样的问题并找到了解决方案:
session.Linq<Theater>()
.Where(x => x.Name.StartsWith("test") && x.Name.EndsWith("test");
Run Code Online (Sandbox Code Playgroud)
这在SQL中转换为
SELECT ... WHERE Name LIKE '%test' AND Name LIKE 'test%'
Run Code Online (Sandbox Code Playgroud)
使用NH 4(可能稍早一点),命名空间Like
内有一个内置的字符串扩展名NHibernate.Linq
:Like(this string matchExpression, string sqlLikePattern)
.(它在NHibernate.Linq.SqlMethods
扩展类上定义.)
using NHibernate.Linq;
...
session.Query<Theater>()
.Where(t => t.Name.Like("test"));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16939 次 |
最近记录: |