NHibernate.Linq LIKE
Cómo puedo producir esta consulta usando NHibernate.¿Linq?
WHERE this_.Name LIKE @p0; @p0 = 'test' // Notice NO % wild card
Tenga en cuenta que esto no es Linq Para Sql o Entity Framework. Este es NHibernate.
Editar:
Aquí está la consulta deseada usando ICriteria:
criteria.Add(Expression.Like("Name", "test"));
return criteria.List<Theater>();
4 answers
Con NH 4 (y probablemente un poco antes), una extensión de cadena incorporada Like
está disponible dentro del espacio de nombres NHibernate.Linq
: Like(this string matchExpression, string sqlLikePattern)
. (Se define en NHibernate.Linq.SqlMethods
clase de extensión.)
using NHibernate.Linq;
...
session.Query<Theater>()
.Where(t => t.Name.Like("test"));
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-24 15:30:59
Si bien esto se ha marcado como resuelto, lo cual era correcto en ese momento, también puedo señalar que NHibernate tiene algunas extensiones ahora para que pueda hacer lo siguiente:
Session.QueryOver<MyEntity>()
.Where(x => x.Property.IsLike("something", MatchMode.Anywhere))
.List();
Esto hará un LIKE '%something%'
para usted.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-10-24 16:19:17
Creo que esto es lo que estás buscando:
var theaters = from theater in Session.Linq<Theater>()
where theater.Name.Contains("test")
select theater;
De acuerdo con mis pruebas genera una sentencia SQL 'LIKE' : "... DONDE theater.Name COMO % test% "
Que es exactamente la salida del fragmento de criterio que ha proporcionado.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-11-08 22:15:41
Tuve el mismo problema en mi proyecto y encontré una solución :
session.Linq<Theater>()
.Where(x => x.Name.StartsWith("test") && x.Name.EndsWith("test");
Esto se traduce en SQL a
SELECT ... WHERE Name LIKE '%test' AND Name LIKE 'test%'
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-30 19:24:25