NHibernate Effectively Removing Using LINQ Where Condition

Having a repository for NHibernate with LINQ queries like

var q = from x in SomeIQueryable<SomeEntity> where x.A1 == a1 && x.B1 == b1 select x; 

Is there a solution how to get this WHERE filter and apply it for "one-shot-delete", which, apparently, is possible only through HQL:

 var cmd = string.Format("delete from SomeEntity where x.A1 = '{0}' and x.B1 = {1}", a1, b1); session.CreateQuery(cmd).ExecuteUpdate(); 
+8
c # linq nhibernate
source share
4 answers

The NH LINQ provider and the criteria / query API do not support conditional deletes / updates. HQL or raw SQL are the only options if you are not learning the NHibernate extension.

+6
source share

Currently, starting with NH 4.0.1, this is not possible. However, there is an open question in Jira (NH-3659, https://nhibernate.jira.com/browse/NH-3659 ). There is a hacking solution based on a custom interceptor and SQL replacement described at http://weblogs.asp.net/ricardoperes/strongly-typed-delete-with-nhibernate , but I am working on a clean solution and will eventually send a pull request.

+1
source share
 (from x in NHSession.Query<SomeEntity>() where x.A1 == a1 && x.B1 == b1 select x).ForEach(y => { NHSession.Delete(y) }); NHSession.Flush(); 
0
source share

Now it is possible with Nhibernate 5.0:

 session.Query<SomeIQueryable>() .Where(x => x.A1 == a1 && x.B1 == b1) .Delete(); 

Documentation:

  // // Summary: // Delete all entities selected by the specified query. The delete operation is // performed in the database without reading the entities out of it. // // Parameters: // source: // The query matching the entities to delete. // // Type parameters: // TSource: // The type of the elements of source. // // Returns: // The number of deleted entities. public static int Delete<TSource>(this IQueryable<TSource> source); 
0
source share

All Articles