IEnumerable not enumerating in foreach

断了今生、忘了曾经 提交于 2019-12-04 15:46:08

So it looks like the problem was to do with the dependencies collection infinately depending on itself.

It seems from my debugging that iterating the IEnumerable causes a timeout and so the foreach simply skips execution of its contents where as ToList() returns as much as it can before timing out.

I may not be correct about that but it's what seems to be the case as far as I can tell.

To give a bit of background as to how this all came about I'll explain the code changes I made yesterday.

The first thing the application does is build up a collection of all resources which are filtered by resource type. These are being brought in from our CMDB via a web service call.

What I was then doing is for each resource that was selected (via autocomplete in this case) I'd make a web service call and get the dependents for the resource based on its Guid. (recursively)

I changed this yesterday so that we didn't need to obtain the full resource information in this second web service call, rather, simply obtain a list of Guids in the web service call and grab the resources from our resources collection.

What I forgot was that the web service call for dependents wasn't filtered by type and so it was returning results that didn't exist in the original resources collection.

I need to look a bit further but it seems that at this point, the new collection of dependent resources was becoming dependent on itself and thus, causing the IEnumerable<IDependents> collection later on to timeout.

This is where I've got to today, if I find anything else I'll be sure to note it here.

To summarise this:

If infinite recursion occurs in an IEnumerable it'll simply timeout when attempting to enumerate in a foreach.

Using ToList() on the IEnumerable seems to return as much data as it can before timing out.

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