Basically I'm trying to convert from:
keys[3] = {"id", "name", "comment"}
values[3] = {"1", "Ackerman", "superuser"}
to:
new { id: "1", name: "Ackerman", comment: "superuser"}
How can I do this?
Answer: Use Dot Notation or Square Bracket You can simply use the dot notation ( . ) to add a key/value pair or a property to a JavaScript object.
ToList() Method. A simple solution is to use ToList() method to create a List<KeyValuePair> from a Dictionary<TKey,TValue> . It is available in LINQ and you need to add System. Linq namespace.
You can create custom dynamic objects by using the classes in the System. Dynamic namespace. For example, you can create an ExpandoObject and specify the members of that object at run time. You can also create your own type that inherits the DynamicObject class.
I think wat you want is an Expando object which allows to add dinamically properties:
keys[3] = {"id", "name", "comment"}
values[3] = {1, "Ackerman", "superuser"}
dynamic item = new ExpandoObject();
var dItem = item as IDictionary<String, object>;
for(int buc = 0; buc < keys.Length; buc++)
dItem.Add(keys[buc], values[buc]);
After that you can call your object as you will do with any other:
var id = item.id;
var comment = item.comment;
item.name = "new name";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With