1

I have a problem in a Linq-Where method. I get a NullReferenceException in a where clause, which should not happen because C# should use short-circuiting and the second operations hould not be executed:

enter image description here

If Item is null, Item.State == ... should not be called, because the condition is already true (short-circuiting).

But it seems, that short-circuiting does not working in this case.

Does anyone else had and solved this problem? Thank you!

Edit: In the end, the connectionList should not contains any null-values and no broken connections.

BendEg
  • 20,098
  • 17
  • 57
  • 131

1 Answers1

0

This is common problem, when querying against databases. Namely, translating short-circuiting behavior against databases, don't work the way as you would expect. You can read more about such behavior: The || (or) Operator in Linq with C#

You could try this:

connectionList.RemoveRange(connectionList.Where(x => x==null));
connectionList.SaveChanges();
connectionList.RemoveRange(connectionList.Where(x => x.Item==BrokenState));

just to see if it works.

Community
  • 1
  • 1
Erti-Chris Eelmaa
  • 25,338
  • 6
  • 61
  • 78