我正在尝试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?