Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq使用ContainesList而不是AddArrayParameters移除所有_C#_Linq_.net Core_Entity Framework Core - Fatal编程技术网

C# Linq使用ContainesList而不是AddArrayParameters移除所有

C# Linq使用ContainesList而不是AddArrayParameters移除所有,c#,linq,.net-core,entity-framework-core,C#,Linq,.net Core,Entity Framework Core,如何将此SQL命令查询转换为实体框架Linq?正在尝试删除包含列表 努力利用资源 public async Task<int> PurgeInventory(IEnumerable<int> ids) { var command = new SqlCommand("Update [dbo].[Inventory] set status = 0 where InventoryId in ({ids})"); command.

如何将此SQL命令查询转换为实体框架Linq?正在尝试删除包含列表

努力利用资源

    public async Task<int> PurgeInventory(IEnumerable<int> ids)
    {
        var command =  new SqlCommand("Update [dbo].[Inventory] set status = 0 where InventoryId in ({ids})");
        command.AddArrayParameters("ids", ids);
        return await UnitOfWork.ExecuteSqlCommandAsync(command);
    }
公共异步任务PurgeInventory(IEnumerable ID)
{
var command=newsqlcommand(“更新[dbo].[Inventory]设置状态=0,其中InventoryId位于({ids})”;
AddArrayParameters(“ids”,ids);
返回等待UnitOfWork.ExecuteSqlCommandAsync(命令);
}
我的尝试-查找修复语法:

   IEnumerable<int> ids = new IEnumerable<int>();

   UnitOfWork.DBSet<Inventory>().Remove(x => Where(x => InventoryId.Contains(ids));
IEnumerable id=new IEnumerable();
DBSet().Remove(x=>Where(x=>InventoryId.Contains(id));

资源:

对于大批量删除操作,通常最好将其作为原始SQL语句来执行,但是如果要删除的行数可管理,则:

(快速)

IEnumerable ids=getIdsToDelete();
var idRecords=ids.Select(x=>newinventory{InventoryId=x}).ToList();
使用(var context=new MyDbContext())
{
foreach(idRecords中的变量idRecord)
{
context.Inventory.Attach(idRecord);
context.Inventory.Remove(idRecord);
}
SaveChanges();
}
这需要一个新的上下文实例,该实例可能不会加载任何库存记录,并且需要选择要删除的库存ID不包含重复项。我们将ID转换为虚拟库存实体,然后将其附加到DbContext,并告诉EF删除它们

(彻底的)

IEnumerable ids=getIdsToDelete();
使用(var context=new MyDbContext())
{
var idRecords=context.Inventory.Where(x=>ids.Contains(x.InventoryId)).ToList();
foreach(idRecords中的变量idRecord)
context.Inventory.Remove(idRecord);
SaveChanges();
}
这一个从上下文中检索实体,然后将其删除。如果您希望更新相关记录或以其他方式管理关系,请记住
。Include()
这些关系就是最好的方法

“你唯一应该在标题中使用标记的时候,是当它们与标题的对话基调有机结合时。”
IEnumerable<int> ids = getIdsToDelete();
var idRecords = ids.Select(x => new Inventory { InventoryId = x }).ToList();

using (var context = new MyDbContext())
{
   foreach(var idRecord in idRecords)
   {
      context.Inventory.Attach(idRecord);
      context.Inventory.Remove(idRecord);
   }
   context.SaveChanges();
}
IEnumerable<int> ids = getIdsToDelete();

using (var context = new MyDbContext())
{
   var idRecords = context.Inventory.Where(x => ids.Contains(x.InventoryId)).ToList();

   foreach(var idRecord in idRecords)
      context.Inventory.Remove(idRecord);

   context.SaveChanges();
}