Short-circuiting in Linq-Where

馋奶兔 提交于 2019-12-12 03:17:07

问题


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:

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.


回答1:


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.



来源:https://stackoverflow.com/questions/27900005/short-circuiting-in-linq-where

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!