2
 //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();
4

3 回答 3

3

这确实是一个错误,并在 LINQ 4.0 中得到纠正

http://damieng.com/blog/2009/06/01/linq-to-sql-changes-in-net-40

查询稳定性 Contains 现在检测自引用 IQueryable 并且不会导致堆栈溢出

EDIT In .NET 3.5 to resolve the problem: When using 'Auto Generated Value' = True, then you must set 'Delay Loaded' to False - otherwise you get the recursion error.

EDIT2 The solution above didn't work.

于 2009-07-14T17:34:31.433 回答
0

在最后一行设置断点,然后运行代码。然后当它在断点处停止时,在该代码块的开头添加另一个断点。然后在调试器中继续(跳过)。它应该到达新的断点,您可以检查堆栈以查看它是如何回调自身的。

属性的实现是Operation什么?也许这会以递归方式回调到其他代码。

于 2009-07-13T14:20:28.987 回答
0

I know this is an old post already, but changing the Contains method to Equals worked for me also.

This fails with StackOverflowException

Dim iLottery As IEnumerable(Of Lottery) = From lottery2 In combined2 Where Not (From lottery1 In combined Select lottery1.NUMBER).Contains(lottery2.NUMBER) Select lottery2

This does not

Dim iLottery As IEnumerable(Of Lottery) = From lottery2 In combined2 Where Not (From lottery1 In combined Select lottery1.NUMBER).Equals(lottery2.NUMBER) Select lottery2

于 2013-01-16T20:42:51.367 回答