2

We have 4ItemInfos in the megaItems:

IEnumerable<ItemInfo> megaItems;

After Doing one of the below lines we have 0 ItemInfos

var array = megaItems.ToArray();
//var array = megaItems.Cast<ItemInfo>().ToArray();
return array;

How to do this conversion the proper way ?

LastBye
  • 1,143
  • 4
  • 19
  • 37
  • Your code looks OK. Are you sure you have anything in the `megaItems` variable, and that it is not an empty sequence ? – driis Feb 24 '13 at 13:22
  • How do you know that you have 4 items in the first place? What does `megaItems.Count()` return? That's the number of items you have in your `IEnumerable`. – Darin Dimitrov Feb 24 '13 at 13:22
  • 3
    Why do you have a `Cast` there? – Oded Feb 24 '13 at 13:23
  • QuickWatch shows 4 items in the list, One line below after converting ToArray(), All are gone! – LastBye Feb 24 '13 at 13:24
  • What list are you talking about? An `IEnumerable` is not a list! You cannot know how many items you have in an IEnumerable by looking at the QuickWatch. The reason for that is very simple. You do not know in advance the number of items in an IEnumerable until you start enumerating over them. – Darin Dimitrov Feb 24 '13 at 13:25
  • Casting was just for a try to see if there would be any changes in the result or not. – LastBye Feb 24 '13 at 13:26
  • So if `megaItems.Count()` returns 0, that's the exact number of items you have. So what's your actual question? What are you trying to achieve? How is this `megaItems` variable populated? – Darin Dimitrov Feb 24 '13 at 13:27
  • By the List I meant : "megaItems", After watching that it shows the 4 Items as expected. – LastBye Feb 24 '13 at 13:27
  • megaItems.Count() is 4, array.Count is 0. – LastBye Feb 24 '13 at 13:28
  • 1
    Could you post a code where you fill megaItems? – Alexeyss Feb 24 '13 at 13:28
  • 2
    `megaItems.Count() is 4, array.Count is 0` - that's very hard to believe. And I don't believe it. Could you show a short and yet complete example allowing to prove that? – Darin Dimitrov Feb 24 '13 at 13:29
  • Let me check which part I can select to post. – LastBye Feb 24 '13 at 13:30
  • 1
    Ideally write a short console application allowing to reproduce and illustrate the problem. – Darin Dimitrov Feb 24 '13 at 13:31
  • try doing `array.Count` first, and then `megaItems.Count()` and see what results you get. I think it's possible that the enumeration changes the data source. – Cristian Lupascu Feb 24 '13 at 13:32
  • @Darin Dimitrov I agree, that is the process I do most when I can't figure out an issue. – LastBye Feb 24 '13 at 13:38
  • @w0lf , OK I'll do some debug Outputs of the Counts of each. – LastBye Feb 24 '13 at 13:38
  • As a side note: I wouldn't touch `ReflectionOnlyLoad`. Consider using [Mono.Cecil or CCI](http://stackoverflow.com/a/11164581/445517). I found `ReflectionOnlyLoad` a pain to work with in so many ways whereas Mono.Cecil simply worked. – CodesInChaos Feb 24 '13 at 13:43
  • Found the Issue: I think most of you were right, the Module's Count returned 0, IEnumerable ModuleInfo's source in the QuickWatch shows 4 items but as a result view shows 0, How can I make this IEnumerable Source to be available as the result. – LastBye Feb 24 '13 at 13:46
  • @CodesInChaos Will try that also, but is there any help or good example on it, there is a while I want to try this great tool but haven't the chance yet. – LastBye Feb 24 '13 at 13:52
  • I think I should select from the modules via a Linq query and then use the ToArray to get the result. – LastBye Feb 24 '13 at 13:56

3 Answers3

4

Cast (unlike OfType) does not remove items - so we can probably assume it isn't that. My guess is that the "mega items" is actually reporting different results during subsequent iterations (very possible; from your link,maybe iterating them causes them to become loaded - therefore no longer not non-loaded). I would guess that if we do:

var x = megaItems.ToArray();
var y = megaItems.ToArray();

then (my guess) x has length 4, and y has length 0. If this is the case, then simply avoid reading it twice, or buffer the output the first time.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Sounds quite plausible given the name `GetNotAllreadyLoadedModuleInfos()`. – Martin Liversage Feb 24 '13 at 13:41
  • @Martin indeed the use of "in the" in the opening line of the question suggests that the OP is thinking of the sequence as though it is a collection, but that simply is not always the case. – Marc Gravell Feb 24 '13 at 13:44
  • Hi and thanks Mark for the info, the problem is that IEnumerable Collection shows the gathered items in it's source in the watch, and not as a resultview, don't know why and how can I get it as a final result. Could you please help me on this – LastBye Feb 24 '13 at 13:49
  • @LastBye why do you keep implying that the result is a collection? Do you actually know that to be the case? Because: it does not need to be. A sequence does not guarantee it is repeatable. Ideally you should only try to consume them exactly once. I suspect it is an iterator block, and that iterating it has side effects - specifically, causing the modules to become loaded. – Marc Gravell Feb 24 '13 at 13:59
  • Sorry but didn't understand what 'result' are you mentioning. The code is quite heavy, during the iteration it collected 4 modules which weren't loaded at the initial beginning and it's understandable cause I removed the reference to them intentionally which I expected those assemblies be gathered here. During the iteration and the process of "GetNotAllreadyLoadedModuleInfos" the assemblies found as expected. When I do QuickWatch I can see they are there but not as a "resultview", I could see them in the "source" part. Yes I'm sure the assemblies are the right choices, but don't know why -> – LastBye Feb 24 '13 at 14:10
  • they weren't known as the results. I think I should ask a new question about the topic with the related code Why "GetNotAllreadyLoadedModuleInfos" can't bring the modules as a result. Thanks again Marc Gravell. – LastBye Feb 24 '13 at 14:12
  • @lastBye I mean: whatever is megaItems, which obviously depends on the GetNot...Info method. I do not think it is a collection. I suspect it uses "yield return", making it an "iterator block". – Marc Gravell Feb 24 '13 at 15:29
  • http://pastebin.com/NcaxL8dw , this is the final returning part from GetNot...Info method, the Line validAssemblies.Add(fileInfo); Is hitting 4 times and the validAssemblies have 4 items this misbehavior seems to be happen after this conditional Linq Select. Which I don't know how exactly to simplify or fix that in a way it returns all the 4 members to be shown as a resultView of this IEnumerable returning method. – LastBye Feb 24 '13 at 15:46
  • @LastBye question: the ToArray twice (as per my answer) - does the second have 0? or 4? elements? – Marc Gravell Feb 24 '13 at 15:53
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/25043/discussion-between-lastbye-and-marc-gravell) – LastBye Feb 24 '13 at 16:01
  • Let me edit some parts in the question which would clarify more. – LastBye Feb 24 '13 at 16:10
1

Overriding GetHashCode() and Equals(object obj) of ItemInfo Class may solve the problem

Mohsen Heydari
  • 7,256
  • 4
  • 31
  • 46
0

The array most probably isn't empty. Try using the items instead of looking in quick watch. Here is the quote from MSDN

This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.

VladL
  • 12,769
  • 10
  • 63
  • 83