I am trying to delete rows from a table using where column in (collection) using the following method:
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));
}
The problem is that yearsAsCommaSeperatedString is a string, therefor the db can not interpret it the numbers. I’ve also tried adding the list of integers as the parameter, but NHibernate does not know what to do with it.
How can i use where in(collection) with CreateSQLQuery?
You can use something like this
Or you can try this