2

I am trying to count the dynamic properties of an ExpandoObject.

I have tried

int count = values.ICollection<KeyValuePair<string, Object>>.Count();

but it generates error.

Any help?

UPDATE:

how can i read a property at specific index?

s.k.paul
  • 7,099
  • 28
  • 93
  • 168
  • That doesn't look like valid syntax in the first place. What is the error you are getting? What is the dynamic object instance (`values`)? – Christian.K May 20 '14 at 07:08

2 Answers2

4

Your cast is wrong. Instead of

int count = values.ICollection<KeyValuePair<string, Object>>.Count();

use

int count = ((ICollection<KeyValuePair<string, Object>>)values).Count;

You could also cast your object to IDictionary<string, object>, then it becomes shorter:

int count = ((IDictionary<string, object>)values).Count

To read a property at specific index, you can use the ElementAt extension method:

var valueAt5 = ((IDictionary<string, object>)values).ElementAt(5).Value
sloth
  • 99,095
  • 21
  • 171
  • 219
  • Wrong: Count is a member of `IEnumerable`. It should be `Count` "as is" :D – Matías Fidemraizer May 20 '14 at 07:10
  • @MatíasFidemraizer `Count` is a property of `ICollection`. You're talking about `Count` extension method – Sriram Sakthivel May 20 '14 at 07:11
  • @sloth, please do me another favour- how can i read a property at specific index? – s.k.paul May 20 '14 at 07:16
  • Well, you should post a *new* question about this, but since it's quite easy to do: after casting to `ICollection....` or `IDictionary....`, use the `ElementAt` extension method: `((ICollection>)values).ElementAt(1).Value` – sloth May 20 '14 at 07:20
  • Is it even well-defined which value is the fifth one, given that you can both add and remove values? It is not clearly guaranteed in the spec which order the values will be enumerated in. For example, what order do you expect here: `dynamic values = new System.Dynamic.ExpandoObject(); values.Hello = 1; values.how = 2; values.are = 3; ((IDictionary)values).Remove("how"); values.you = 4; values.how = 5; foreach (var pair in values) { Console.WriteLine(pair); }`. I can reveal that it is different from the order of a non-dynamic `Dictionary<,>` with the samme adds and removes. – Jeppe Stig Nielsen May 20 '14 at 08:38
  • I don't know if it makes sense get a property by an index (you'll have to ask the OP). Nonetheless, the `ExpandoObject` is quite different from a regular `Dictionary<,>`. While the order of items in a `Dictionary<,>` is non-deterministic due to the hashing of the keys, the keys of a `ExpandoObject` are kept in order (also, removed and later re-added keys are going to be reused). So for your example, I would expect the following order: `Hello:1`, `how:5`, `are:3`, `you:4` since the key `how` was added as second property. – sloth May 20 '14 at 09:21
1

ExpandoObject implements IDictionary<string, object>, thus you need to cast it to this interface and you'll be able to access IDictionary<TKey, TValue>.Count.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206