从 LINQ 结果中删除 NHibernate

Che*_*rot 3 linq nhibernate

我想知道 nhibernate 中删除方法的最佳用法。

如果你去实体不仅仅是调用 delete 并发送它,但如果不是你需要查询它或编写查询并将其发送到 delete 方法。

我想知道是否可以编写一个 linq 表达式并将其发送给删除。

是否可以对 hql 执行 Linq 转换,然后使用生成的 hql 调用 session.Delete(query)?

我想调用 Session.Delete,并给它一个 linq,这样它就可以知道要删除什么而不选择数据。你知道一个可以将 linq 表达式转换为 hql 的类吗?

Sÿl*_*Sÿl 5

您现在可以直接在 linq 中使用 NHibernate 5.0

    //
    // 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)

例子:

        var tooOldDate = System.DateTime.Now.AddYears(5);
        session.Query<User>()
            .Where(u => u.LastConnection <= tooOldDate)
            .Delete();
Run Code Online (Sandbox Code Playgroud)