gpt4 book ai didi

c# - NHibernate 高效删除使用 LINQ Where 条件

转载 作者:可可西里 更新时间:2023-11-01 09:05:38 26 4
gpt4 key购买 nike

有一个 NHibernate 的存储库,带有这样的 LINQ 查询

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

是否有解决方案如何获取此 WHERE 过滤器并将其应用于“一次性删除”,这似乎只能通过 HQL 实现:

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

最佳答案

现在可以使用 Nhibernate 5.0 了:

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

文档:

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

关于c# - NHibernate 高效删除使用 LINQ Where 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7031609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com