我在C#中有几个SQL语句,我需要添加一个范围,以便我的SQL语句类似于:
DELETE FROM TABLE WHERE SOMETHING IN (value1, value2, value3 , ...);
我有一个对象列表,其中包含我想要替换值的id。如果没有针对每个陈述,我将如何解决这个问题?
我有三个类ID为12,2和5的类:
public class inventoryItems
{
public string id { get; set; }
}
答案 0 :(得分:0)
如果你使用直接的ADO.NET代码,你可以做类似的事情来生成T-SQL:
class Program
{
public class InventoryItems
{
public string Id { get; set; }
}
static void Main(string[] args)
{
var items = new[]
{
new InventoryItems {Id = "One"},
new InventoryItems {Id = "Two"},
new InventoryItems {Id = "Three"},
};
var idsToDelete = string.Join(",", items.Select(x => "'" + x.Id + "'"));
var deleteStatement = $"DELETE FROM InventoryItem WHERE Id IN ({idsToDelete})";
Console.WriteLine($"TSQL - {deleteStatement}");
Console.ReadLine();
}
}