Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.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# 为什么dataContext.GetChangeSet().Deletes.Count()总是返回0?_C#_Asp.net_Linq_Linq To Sql - Fatal编程技术网

C# 为什么dataContext.GetChangeSet().Deletes.Count()总是返回0?

C# 为什么dataContext.GetChangeSet().Deletes.Count()总是返回0?,c#,asp.net,linq,linq-to-sql,C#,Asp.net,Linq,Linq To Sql,我有这样的想法: var customerIdsToDelete = new {1, 2, 3}; var dataContext = new DataContext(); var customersToDelete = (from c in data.Customers where customerIdsToDelete.Contains(c.CustomerID) select c); da

我有这样的想法:

var customerIdsToDelete = new {1, 2, 3};
var dataContext = new DataContext();
var customersToDelete = (from c in data.Customers
                         where  customerIdsToDelete.Contains(c.CustomerID)
                         select c);
data.Customers.DeleteAllOnSubmit(customersToDelete);
data.SubmitChanges();

var deletedCount = data.GetChangeSet().Deletes.Count();
即使成功删除客户,
deletedCount
也将为0


为什么??计算被删除的
客户数的“正确”方法是什么?

因为您已经提交了更改。一旦调用了
SubmitChanges
,则不存在删除,因此(正确地)返回零计数。要获取已删除(或要删除)的项目数,请尝试此操作(即在推送更改之前确定计数):


SubmitChanges方法是否提供接受回调的重写

我曾经使用过Silverlight和RIA服务,DomainContext的SubmitChanges方法覆盖了表单
SubmitChanges(Action,object userState)

我们称之为:

mycontext.SubmitChanges( submitOperation => 
{
     //here, inside the callback, you can inspect
     //the submitOperation for the results
     //of the SubmitChanges method
}, null);

hrm,如果由于某种原因更改集中的某个客户没有被删除(可能有一些数据库约束阻止了这一点),是否仍有其他方法来确定?是的,您的应用程序将遇到异常,假设这在异常情况下会发生。
mycontext.SubmitChanges( submitOperation => 
{
     //here, inside the callback, you can inspect
     //the submitOperation for the results
     //of the SubmitChanges method
}, null);