1

how to convert : A List :

var list = new List<string>(){"str1","str2"}

to a anonymous object :

var anonymousObject = new {str1 = "str1",str2 = "str2"}

during runtime

10
  • What problem are you trying to solve by doing this? Commented May 18, 2016 at 0:06
  • Just use a Dictionary<string, object>. Commented May 18, 2016 at 0:08
  • i'm trying to join two data tables using lambda expressions. when i have to join on a single column its easy and it looks like this <code> Table1.Join(Table2, leftTable => leftTable["joincolumn"], rightTable => rightTable["joincolumn"], (leftTable,rightTable) => new {leftTable,rightTable} ) </code> But i have an list in place of "joinColumn" and this needs to be handled using a dynamic object <code> new {column1=leftTable.column1,column2=leftTable.column2} </code> and my list has column1 and column2 Commented May 18, 2016 at 0:08
  • Do you want to slice List items into object of two properties? Commented May 18, 2016 at 0:13
  • stackoverflow.com/questions/3549103/… check this link Commented May 18, 2016 at 0:13

2 Answers 2

3

You can use the ExpandoObject which will give you the feature of dynamic type.

        var list = new List<string>() { "str1", "str2" };
        ExpandoObject obj = new ExpandoObject();
        var store = (IDictionary<string, object>)obj;

        list.ForEach(x => store.Add(x, x));

        dynamic lst  = obj;
        var val = lst.str1; // Test
Sign up to request clarification or add additional context in comments.

Comments

0

You can also use extension method represented below (from here).

Because converting list to dynamic object by iterating on items manually can be painful when there is many situations like this in your application.

You can use this extension method like this:

  dynamic list = new List<string>() { "str1", "str2" }
       .ToDictionary(dd => dd, dd => (object)dd)
       .ToExpando();

The extension method:

    public static ExpandoObject ToExpando(this IDictionary<string, object> dictionary)
    {
        var expando = new ExpandoObject();
        var expandoDic = (IDictionary<string, object>)expando;

        // go through the items in the dictionary and copy over the key value pairs)
        foreach (var kvp in dictionary)
        {
            // if the value can also be turned into an ExpandoObject, then do it!
            if (kvp.Value is IDictionary<string, object>)
            {
                var expandoValue = ((IDictionary<string, object>)kvp.Value).ToExpando();
                expandoDic.Add(kvp.Key, expandoValue);
            }
            else if (kvp.Value is ICollection)
            {
                // iterate through the collection and convert any strin-object dictionaries
                // along the way into expando objects
                var itemList = new List<object>();
                foreach (var item in (ICollection)kvp.Value)
                {
                    if (item is IDictionary<string, object>)
                    {
                        var expandoItem = ((IDictionary<string, object>)item).ToExpando();
                        itemList.Add(expandoItem);
                    }
                    else
                    {
                        itemList.Add(item);
                    }
                }

                expandoDic.Add(kvp.Key, itemList);
            }
            else
            {
                expandoDic.Add(kvp);
            }
        }

        return expando;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.