-1

I have a list of nodes, each node has a reference to its previous node and its next node. I want a list of the nodes which do not share the next or the previous ones. In other words, a list of nodes that are not sequenced.

 nodes= nodes.Where(o => !nodes.Any(oo => ReferenceEquals(oo.NextNode, o.PreviousNode)));

It throws this exception

An unhandled exception of type 'System.StackOverflowException' occurred in NodeLib.dll

I don't know what I am missing or misunderstanding. Thank you in advance!

Madonna Remon
  • 1,149
  • 1
  • 7
  • 12
  • Could be caused by the implementation of the enumerator or properties. Hard to tell without more info. – marsze Nov 08 '18 at 10:46
  • 1
    This feels like an issue with recursion somewhere, i.e. one of your nodes is referencing a `PreviousNode` that references a `PreviousNode` that then, ultimately, references the original. Just speculation at this point, though. –  Nov 08 '18 at 10:50
  • @marsze How could that happen? correct me if I am wrong, but I am only comparing if the nodes are shared or not by reference. and I get the exception in different dlls each time I run the program. It is not always in the `NodeLib.dll` – Madonna Remon Nov 08 '18 at 10:50
  • what type is your `nodes` object? – meJustAndrew Nov 08 '18 at 10:50
  • It may help to post your nodes class code as well and the code that sets up the nodes list structure – auburg Nov 08 '18 at 10:54
  • @JᴀʏMᴇᴇ you are right about your guess. But I do not get it, shouldn't it only compare the `PreviousNode` and stop? and if that is the case, then which linq function should I use? – Madonna Remon Nov 08 '18 at 10:55
  • I mean does that class implement `IEnumerable` and changes the current enumerator that it has when accessing `NextNode` and `PreviousNode`? This would explain the stackoverflow exception, because it will end up moving back and forth between two nodes... – meJustAndrew Nov 08 '18 at 10:55
  • Try the following: `var test = from i in nodes from j in nodes.SkipWhile(j => j != i) where i != j select ReferenceEquals(i.NextNode, j.PreviousNode);` – Nekeniehl Nov 08 '18 at 10:57
  • @MadonnaRemon - yes, that is all it _appears_ to do. But there might be some further details in the `getter` of the nodes that we're not aware of, for example. Or something else. Do you have visibility of the `Node` type? –  Nov 08 '18 at 10:57
  • @meJustAndrew not it does not implment `IEnumerable`. – Madonna Remon Nov 08 '18 at 10:58
  • @JᴀʏMᴇᴇ the `NextNode` property and the `PreviousNode` property have normal `setter` and `getter` functions, they only retrieve the private field. – Madonna Remon Nov 08 '18 at 11:12
  • It would be awesome if you could provide a [mcve]. – mjwills Nov 08 '18 at 11:34

1 Answers1

1

I think your problem is the way you are comparing the list, you want to compare the list with itself, try the following:

var test = from i in nodes
    from j in nodes.SkipWhile(j => j != i)
    where i != j
    select ReferenceEquals(i.NextNode, j.PreviousNode);

Link -> Efficient list self compare in LINQ?

Nekeniehl
  • 1,633
  • 18
  • 35
  • This returns a list contains one `Boolean` only for a list that has two nodes. – Madonna Remon Nov 08 '18 at 11:07
  • You should be able to modified accordinly to get the result you want with the provided link. Your problem is that you are self comparing the list, when the nodes are not sequenced, which one you choose? `o`, `oo` or both?. I will suggest to go first with normal loops and run over the list itself, then modfied to be Linq if you need to. – Nekeniehl Nov 08 '18 at 11:41