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

时间:2011-02-16 00:24:00

标签: 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);
data.Customers.DeleteAllOnSubmit(customersToDelete);
data.SubmitChanges();

var deletedCount = data.GetChangeSet().Deletes.Count();

即使客户被成功删除,deletedCount也将为0。

为什么呢?什么是计算customers删除数量的“正确”方法?

2 个答案:

答案 0 :(得分:3)

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

...

var deletedCount = data.GetChangeSet().Deletes.Count();
data.SubmitChanges();

...

答案 1 :(得分:0)

您的SubmitChanges方法是否提供了一个需要回调的覆盖?

我在Silverlight和RIA Services上做了一些工作,DomainContext的SubmitChanges方法覆盖了SubmitChanges(Action<SubmitOperation>, object userState)

形式

我们称之为:

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