-6

I have a dictionary collection, string, int. I want convert this dictionary to anonymous object with properties that are collection keys and anonymous object propertie's values are dictionary values. Is it a way to do it?

Thank you

Pimpy
  • 49
  • 1
  • 1
  • 10
  • Have you tried doing anything? – Yuval Itzchakov May 12 '15 at 07:52
  • But why? Why it has to be anonymous type? I'm missing how that will be useful – Sriram Sakthivel May 12 '15 at 07:54
  • What would you use it for, that you can't use the dictionary for (even easier)? – Guffa May 12 '15 at 07:55
  • possible duplicate of [How to create Dynamic objects in C#](http://stackoverflow.com/questions/19697506/how-to-create-dynamic-objects-in-c-sharp) – Cwt May 12 '15 at 07:56
  • I habe a layer that works with object properties and with their values. – Pimpy May 12 '15 at 08:07
  • I dont want rewrite the layer, so I am looking for a way to make a object. I thought I can make with ling. And I see it is possible only with a more code around. – Pimpy May 12 '15 at 08:23
  • LINQ anonymous objects are defined at compile time, but this Q would require it to happen at runtime. The answer here is to either stick with a dictionary of name-value pairs or `dynamic`. – Richard Nov 11 '15 at 13:45

2 Answers2

2

Are you looking for this?

var keyVals = dict.Select(kv => new { Key = kv.Key, Value = kv.Value });

But i can't imagine a use case for this. Why do you prefer the anonymous type over the already available dictionary?

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
2

Like this :

from x in mydictionairy select new { anonymousKey = x.Key, anonymousValue = x.Value}
MajkeloDev
  • 1,661
  • 13
  • 30