//A query to a local object
var deletionCommands = commands
.Where(a => a.Operation != Operation.Addition)
.Select(a => new { a.Prestador.cod_prestador, a.Prestador.cod_desdobramento })
;
//A Linq-To-SQL query
var toDelete = db.Prestadors
.Where(a => deletionCommands.Contains(new { a.cod_prestador, a.cod_desdobramento }))
;
db.Prestadors.DeleteAllOnSubmit(toDelete);
db.SubmitChanges();
解决问题的唯一方法是显式循环:
foreach (var command in commands)
{
if(command.Operation != Operation.Addition)
{
var toDelete = db.Prestadors
.Where(a =>
a.cod_prestador == command.Prestador.cod_prestador &&
a.cod_desdobramento == command.Prestador.cod_desdobramento
);
db.Prestadors.DeleteAllOnSubmit(toDelete);
}
}
db.SubmitChanges();
答案 0 :(得分:3)
这真的是一个错误,并在LINQ 4.0中得到纠正
http://damieng.com/blog/2009/06/01/linq-to-sql-changes-in-net-40
查询稳定性 Contains现在检测自引用IQueryable并且不会导致堆栈溢出
编辑在.NET 3.5中解决问题:使用“自动生成值”= True时,必须将“延迟加载”设置为False - 否则会出现递归错误。
EDIT2 上述解决方案无效。
答案 1 :(得分:0)
在最后一行放置一个断点,然后运行代码。然后当它在断点处停止时,在该代码块的开头添加另一个断点。然后继续在调试器中(跳过)。它应该点击新的断点,你可以检查堆栈以查看它如何回调自身。
Operation
属性的实施是什么?也许这会以递归的方式回调其他代码。
答案 2 :(得分:0)
我知道这已经是一个旧帖子,但将Contains方法更改为Equals也适用于我。
这与StackOverflowException失败
Dim iLottery As IEnumerable(Of Lottery)=来自lottery2 in combined2 Where Not Not(来自lottery1 In Combined Select lottery1.NUMBER).Contains(lottery2.NUMBER)Select lottery2
这不是
Dim iLottery As IEnumerable(Of Lottery)=来自lottery2 in combined2 Where Not Not(来自lottery1 In Combined Select lottery1.NUMBER).Equals(lottery2.NUMBER)Select lottery2