NHibernate高效删除使用LINQ Where条件

kas*_*anf 11 c# linq nhibernate

有这样的LINQ查询的NHibernate存储库

var q = from x in SomeIQueryable<SomeEntity> where x.A1 == a1 && x.B1 == b1 select x;
Run Code Online (Sandbox Code Playgroud)

有没有一个解决方案如何获得这个WHERE过滤器并将其应用于"一次性删除",这似乎只能通过HQL实现:

var cmd = string.Format("delete from SomeEntity where x.A1 = '{0}' and x.B1 = {1}", a1, b1);
session.CreateQuery(cmd).ExecuteUpdate();
Run Code Online (Sandbox Code Playgroud)

Sÿl*_*Sÿl 8

现在可以使用Nhibernate 5.0:

session.Query<SomeIQueryable>()
            .Where(x => x.A1 == a1 && x.B1 == b1)
            .Delete();
Run Code Online (Sandbox Code Playgroud)

说明文件:

    //
    // 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);
Run Code Online (Sandbox Code Playgroud)


Dmi*_* S. 6

NH LINQ提供程序和条件/查询转换API不支持条件删除/更新.除非您正在考虑扩展NHibernate,否则HQL或原始SQL是唯一的选择.