【发布时间】:2012-08-22 08:25:16
【问题描述】:
我正在尝试使用where column in (collection) 使用以下方法从表中删除行:
public void DeleteRows(int parentId, List<int> years)
{
var yearsAsCommaSeperatedString = ListToCommaSeperatedString(years);
const string query = "DELETE FROM TABLE t WHERE t.PARENT_ID=:Parent AND t.YEAR in(:yearList)";
Session
.CreateSQLQuery(query)
.SetParameter("Parent", parentId)
.SetParameter("yearList", yearsAsCommaSeperatedString)
.ExecuteUpdate();
}
private static string ListToCommaSeperatedString(IEnumerable<int> ints)
{
var aggregate = ints.Aggregate("", (current, i) => current + (i + ", "));
return aggregate.Substring(0, aggregate.LastIndexOf(",", StringComparison.Ordinal));
}
问题是yearsAsCommaSeperatedString 是一个字符串,因此数据库无法解释它的数字。我也尝试添加整数列表作为参数,但 NHibernate 不知道如何处理它。
如何将where in(collection) 与 CreateSQLQuery 一起使用?
【问题讨论】:
标签: c# sql nhibernate