2017-08-24 61 views
-1

我有一對夫婦在C#中的SQL語句,我需要添加一個範圍,使我的SQL語句類似於:SQL WHERE IN子句中包含具有ID的對象列表?

DELETE FROM TABLE WHERE SOMETHING IN (value1, value2, value3 , ...); 

我有一個有ID的對象,我想代替名單價值。對於每一個陳述,我會怎麼做呢?

我有三個班的12,2的ID字符串,5:

public class inventoryItems 
{ 
    public string id { get; set; } 
} 
+2

更多的信息將是有幫助的 - 你使用的ORM?如果是這樣,哪一個? – combatc2

+0

嘗試用ID創建一個字符串。像「'',''...」 – TheVillageIdiot

回答

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(); 
    } 
}