The "Dictionary" object returned by the "ToDictionary" method does not act like it would in C#.
Zarepheth opened this issue ยท comments
As the Subject line says, the Dictionary
object return from the List.ToDictionay(keyFunc, ValueFunc)
method does not act like the Dictionary
objects in .Net. More specifically, I expected to apply the LINQ functions to it, but they are not supported.
Ideally, we would be able to create a Dictionary object directly, rather than going through the List object.
For instance:
const myDict = new Dictionary<TKey, TValue>([
{"Key":key1, "Value":value1},
{"Key":key2, "Value":value2},
{"Key":key3, "Value":value3},
]);
const someKey: TKey;
const myValue: TValue = myDict[someKey];
const myMaxValue = myDict.Max(kvp => kvp.Value);
const valueList = myDict.Select(kvp => kvp.Value).ToList();
const my3rdValue = valueList[2];
This is very similar to #56, because a dictionary is just a JS object, you can't really perform any operation afterwards, in this behaviour the library differs from C# obviously but I should came up with some way of doing it, any ideas? ๐
I tried doing my own implementation after writing my post, but was unable to accomplish a working item within the few hours between posting and end of the business day.
Using TypeScript, I had some issues specifying anything other than number
or string
for the Key type (TKey
in my example). Worse, I had to explicitly set the indexer's type in some places. Although use of type constraints allowed TKey
in other places. From what I've gather when googling around, one might have to implement non-LINQ-standard methods to set and get values on an internal list.
I also came across a couple other LINQ for TypeScript implementations. At least one has a Dictionary
class. Unfortunately, both the other two libraries required that I target ES6 or later. For my current project, I need to target ES5. When I return to work on Monday, I may take a look at that other project's Dictionary implementation. Or maybe see if I can accomplish my task without using a formal LINQ-style Dictionary.
Under the covers, I think the .Net Dictionary
implementation is nearly identical to List<KeyValuePair>
. At any rate, it's pretty easy to switch between them for my server-side code. I was just hoping to accomplish the same in my client-side code.
Nice, thanks for your work and investigation! I think it might be solved using native Map
s, but I'm not sure if TypeScript can compile those down to es3/es5 ๐ข
@Zarepheth you are soooo going to love tomorrow's PR... โค๏ธ
๐ This issue has been resolved in version 1.12.0 ๐
The release is available on:
Your semantic-release bot ๐ฆ๐