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();
10
  • It's really the call to AsEnumerable that throws an exception? Yikes - normally that doesn't do anything significant. That's without actually iterating over it? Commented Jul 13, 2009 at 14:15
  • @Jon, that was a simplification. Replaced by the actual code. Commented Jul 13, 2009 at 14:22
  • @Jeff now that I solved the problem with an explicit loop, I think it removes the blame from the commands object Commented Jul 13, 2009 at 14:26
  • @Jader, since you've resolved it you may not care anymore, but I'm curious: did you try your original syntax using the equality tests rather than Contains method? That is, did you try .Where(a => a.cod_prestador == deletionCommands.Prestador.cod_prestador && ... instead of .Where(a => deletionCommands.Contains(new { a.cod_prestador, a.cod_desdobramento })) Commented Jul 13, 2009 at 16:10
  • @Jeff No, I have not tried since I see that the "Contains" method is the equivalent for a "direct comparison inside an explicit loop". Commented Jul 13, 2009 at 16:17

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Where is that "auto generated value" and "delay loaded" properties to set?
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.
0

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.

4 Comments

Operation has no implementation (besides {get; set;}) and is an "enum"
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.
Yeah, I think so. If I could reproduce this problem isolating other possible causes I could report it as a bug.
That was really a bug, see my response
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.