cancel
Showing results for 
Search instead for 
Did you mean: 

C# Linq action on dictionary worked in BP but now returns compiler error

Hi everyone,

I build a custom action a couple of days ago where a code stage uses a dictionary (Dictionary<string, DateTime>). 
At some point in this code the "First" item of the dictionary is required, and as a Dictionary is not enumerated (Enumerable? Ordered?) I use the following:

KeyValuePair<string, DateTime> newest = myDictionary.AsEnumerable().First();

AsEnumerable() here functions to enumerate the dictionary, and then the Linq extension First() is used to return one of the dictionary's values. This method worked for a couple of days, then I worked on the action some more (I added a new code stage on the same page) and now it does not function anymore. I get a compiler error saying 

Compiler error at line 32: 'Dictionary<string, DateTime>' does not contain a definition for 'AsEnumerable' and no accessible extension method 'AsEnumerable' accepting a first argument of type 'Dictionary<string, DateTime>' could be found (are you missing a using directive or an assembly reference?)

I have now rewritten the code to use an even more laborious method which seems to work at this moment but I would like to understand what happened here. Does anybody have a tip? I did not change anything in the code options in the meantime so that can't be it.

------------------------------
Thomas Warnaar
------------------------------
6 REPLIES 6

ewilson
Staff
Staff
Hi @Thomas Warnaar,

Bit of a silly question, but have you restarted your BP environment and the machine it's running on since you developed this code? If not, you might give that a try. I've had a few occurrences where something that was working fine suddenly stops (or vice versa) and then a reboot magically fixes the issue.

Cheers,


------------------------------
Eric Wilson
Director, Integrations and Enablement
Blue Prism Digital Exchange
------------------------------

ewilson
Staff
Staff
@Thomas Warnaar,

Thinking about this a bit further, you realize there is no real "First" concept on a Dictionary, right? Dictionaries are unordered, so in theory you could get different KeyValuePairs across multiple attempts to get what you consider first.

With that said, the latest MS documentation clearly shows there is no AsEnumerable method exposed by the Dictionary class, so I'm not sure how this was working for you previously. 🤷‍:male_sign: There is a GetEnumerator method though.

If you expect the data entered into the collection to maintain order, I would switch to using the OrderedDictionary class available in the System.Collections.Specialized namespace.

Cheers,


------------------------------
Eric Wilson
Director, Integrations and Enablement
Blue Prism Digital Exchange
------------------------------

ewilson
Staff
Staff
Ok, thinking about it more after coffee. :face_with_tears_of_joy: AsEnumerable and First do work on a Dictionary because of the implementation of the IEnumerable interface. I was able to break my test by removing either the reference to System.Linq or System.Linq.dll from my Code Options. But as long as those are there, the method (as you described) worked fine. So I'll revert to recommending that you check your Code Options again and reboot.  

I stand by me comment about the ordering though. ;)

Cheers,


------------------------------
Eric Wilson
Director, Integrations and Enablement
Blue Prism Digital Exchange
------------------------------

Hi @ewilson,

Thank you for ​all the replies! I enjoyed seeing the thought process! You are ofcourse absolutely right about the unordered nature of the dictionary. However I just needed a seed for the next bit of code so it was not important which specific entry out of the dictionary was used :) 

But exactly, using the IEnumerable interface should do the trick (and in the start did so as well). I did double check the references and experimented a bit as well but all should be fine. Linq is referenced. Also I fully shutdown my workstation after each workday so rebooting also does not help! In the end I think we may have to jot it down to the ghost in the machine or something haha.

I did not know about the OrderedDictionary class by the way, will definitely use that in the future :thumbs_up:

Thank you!

Sincerely

------------------------------
Thomas Warnaar
------------------------------

Ok, another weird suggestion.
  1. Try removing your references to the Linq namespace and DLL.
  2. Save the VBO
  3. Perform a compile check for errors which should return an Error about the lack of an AsEnumerable method.
  4. Add the references to Linq namespace and DLL back in.
Cheers,

------------------------------
Eric Wilson
Director, Integrations and Enablement
Blue Prism Digital Exchange
------------------------------

Hi all,

I think it is a little bit odd, but you have to include 'System.Core.dll'.
B.t.w. it isn't necessary to use .AsEnumerable() in the mentioned expression. 
myDictionary.First()​
will do the trick,

------------------------------
Tobias Arnold
RPA Developer
ITERGO GmbH
Europe/Duesseldorf
------------------------------