views:

503

answers:

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();

The only thing that solved the problem was an explicit loop:

 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();
A: 

Put a breakpoint on the last line, then run the code. Then when it stops on the breakpoint, add another breakpoint at the start of that chunk of code. Then continue in the debugger (step over). It should hit the new breakpoint, and you can examine the stack to see how it has called back on itself.

What's the implementation of the Operation property? Maybe that calls back into some other code in a recursive way.

Daniel Earwicker
Operation has no implementation (besides {get; set;}) and is an "enum"
Jader Dias
Hmm... then I think your next stop will be Microsoft support... something about your environment is causing the lower layers of Linq To SQL to do something badly wrong.
Daniel Earwicker
Yeah, I think so. If I could reproduce this problem isolating other possible causes I could report it as a bug.
Jader Dias
That was really a bug, see my response
Jader Dias
+2  A: 

That was really a bug, and was corrected in LINQ 4.0

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

Query stability Contains now detects self-referencing IQueryable and doesn't cause a stack overflow

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.

Jader Dias
Where is that "auto generated value" and "delay loaded" properties to set?
yapiskan
When you open the dbml on Visual Studio it opens a visual designer for ORM. Click on a field (inside a class) and open the properties window.
Jader Dias