Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8138615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:32:54+00:00 2026-06-06T11:32:54+00:00

I have an IEnumerable<KeyValuePair<string,string>> , from which I would like, ideally, an anonymous object

  • 0

I have an IEnumerable<KeyValuePair<string,string>>, from which I would like, ideally, an anonymous object which has the keys as property names and the values as property values.

I’ve tried various selection expressions (none of which even compiled…) and an approach using ExpandoObject (see below), but without success. Is there a good way to do this? If possible, I’d like to avoid an extra explicit iteration over the collection (i.e. do it all with a LINQ statement of some sort).

This is what I’ve tried so far. I hope it also clarifies what I’m trying to do:

var kvps = getProps(); // returns IEnumerable<KeyValuePair<string,string>>

dynamic o = new ExpandoObject();
foreach (var kvp in kvps)
{
    o.Add(kvp);
}

This is OK at compile time, but at runtime I get a YSOD stating ‘System.Dynamic.ExpandoObject’ does not contain a definition for ‘Add’ – I guess it works at compile time because o is dynamic, so the compiler can’t know if a method .Add() has been added to it since it was instantiated. The odd thing is, that on the MSDN documenation page for ExpandoObject .Add() is listed as one of several “explicitly implemented interface methods”.

It is not necessary for me to get this into a dynamic object – I just need to get something that has property names and values according to the keys and values of the key-value pairs.

Update: Well, this is embarrassing. Turns out this was something of an XY-problem too.

I’m trying to render this to JSON using the built-in features of ASP.NET MVC, by simply returning Json(data) in my controller. The answers all worked very well to do what I first asked, but when I pass this object as data I still don’t get what I want:

// What I get:
[
   { Key: 'firstkey', Value: 'FirstValue' }, 
   { Key: 'secondKey', Value: 'secondValue' } 
]

// What I want:
{ firstKey: 'FirstValue', secondKey: 'secondValue' }

Apparently, an ExpandoObject with the relevant properties added didn’t cut it – it was cast to a dictionary before rendering…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:32:57+00:00Added an answer on June 6, 2026 at 11:32 am

    ExpandoObject explicitly implements IDictionary<string, object> – so you need to cast it to one first:

    var kvps = getProps();
    dynamic o = new ExpandoObject();
    var dict = o as IDictionary<string, object>;
    foreach (var kvp in kvps)
    {
        dict.Add(kvp.Key, kvp.Value);
    }
    

    Now you can use o as you would expect:

    var example = o.YourKey;
    

    I’m trying to render this to JSON using the built-in features of ASP.NET MVC, by simply returning Json(data) in my controller.

    Interesting.

    To do that, you serialize a dictionary, not an ExpandoObject. MVC 3’s JSON serializer already does that with a dictionary. All you have to do is convert your IEnumerable<KeyValuePair<string, object>> to a dictionary:

    var kvps = getProps();
    var dictionary = kvps.ToDictionary(k => k.Key, v => v.Value);
    return Json(dictionary, JsonRequestBehavior.AllowGet); //take out allow get if you don't need it.
    

    No dynamics required.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an IEnumerable<string > which I would like to split into groups of
I have an object which has a property that contains an IEnumerable of objects.
I have an IEnumerable object. I would like to access based on index for
I have an IEnumerable<Person> object. The Person class has a Designation property. I want
I have an IEnumerable object ( IEnumerable<Class> ) and I would like to retrieve
I have a method that returns an IEnumerable<KeyValuePair<string, ArrayList>> , but some of the
I have an object of the type IEnumerable<KeyValuePair<T,U>> keyValueList , I am using var
I have an IEnumerable< T> which is declared on the page like this: IEnumerable<
I have a IEnumerable. I have a custom Interval class which just has two
I have two IEnumerable collections that I would like to union. One selects news

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.