none
how to initialize a list of ExpandoObject ? RRS feed

  • Question

  • I now have the following code :

    public class Output {
    	public IEnumerable<dynamic> catList;
    	public Output(IEnumerable<dynamic> catList) {
    		this.catList = catList;
    	}
    }
    var catList = new[] { new { name = "toto", age = 5 },
    			new { name = "titi", age = 7 },
    			new { name = "tata", age = 3 },
    			new { name = "tutu", age = 4 } }.ToList();
    Output out = new Output(catList);
    ...
    var results = out;
    foreach (dynamic item in results.catList) {
    	//using item.name and item.age
    }

    And I am looking for transforming my dynamic anonymous type to the use of ExpandoObject. I am looing for the most concise way to declare it. Can you help on that ?

    Thanks in advance,



    • Edited by jmclej Monday, June 8, 2015 1:53 PM
    Monday, June 8, 2015 9:20 AM

Answers

  • "This is what I would like indeed "

    That doesn't work because the type of the expression to be initialized is ExpandoObject and it would need to be dynamic for that to work.

    Probably the best you can do is something like this:

    var catList = new[] { 
        new { name = "toto", age = 5 },
        new { name = "titi", age = 7 },
        new { name = "tata", age = 3 },
        new { name = "tutu", age = 4 } 
    }.Select(x => {
        dynamic d = new ExpandoObject();
        d.name = x.name;
        d.age = x.age;
        return d;
    }).ToList();
    
    Personally I'd probably just use a non-anonymous type...

    Monday, June 8, 2015 10:18 AM
    Moderator

All replies

  • You just add ExpandoObject to all the proper places in the list.

    But I highly doubt that is your real problem. It would make sense that your real problem is "I have a list several times that big and now realised I need expando object"/"I accidently used Anonymous Type Syntax where I meant ExpandoObject on too large scale"

    You could itterate over that list and create a comparable list of ExpandoObject's. But that is a very bad idea as you create something you don't really use (or might mix both up lower in the code).
    If this is source data, you should think about putting it into a file rather then hardcoding it. In any case making it into a type-neutral array might be the better case before you process it to get it's "working type".

    Monday, June 8, 2015 9:42 AM
  • Adding ExpandoObject to all proper places doesn't compile (unless it is not actually at the proper places...). This is what I would like indeed :

    var catList = new System.Dynamic.ExpandoObject[] { new System.Dynamic.ExpandoObject() { name = "toto", age = 5 },
    	new System.Dynamic.ExpandoObject() { name = "titi", age = 7 },
    	new System.Dynamic.ExpandoObject() { name = "tata", age = 3 },
    	new System.Dynamic.ExpandoObject() { name = "tutu", age = 4 } }.ToList();
    But I get "'System.Dynamic.ExpandoObject' does not contain a definition for 'name'" (same error for age of course).

    What is working but that I would like to shorten to the maximum is the following code :

    List<System.Dynamic.ExpandoObject> catList = new List<System.Dynamic.ExpandoObject>(); 
    dynamic NameAge = new System.Dynamic.ExpandoObject();
    NameAge.name = "toto";
    NameAge.age = 5;
    catList.Add(NameAge);
    NameAge = new System.Dynamic.ExpandoObject();
    NameAge.name = "titi";
    NameAge.age = 7;
    catList.Add(NameAge);
    NameAge = new System.Dynamic.ExpandoObject();
    NameAge.name = "tata";
    NameAge.age = 3;
    catList.Add(NameAge);
    NameAge = new System.Dynamic.ExpandoObject();
    NameAge.name = "tutu";
    NameAge.age = 4;
    catList.Add(NameAge);

    Monday, June 8, 2015 10:02 AM
  • "This is what I would like indeed "

    That doesn't work because the type of the expression to be initialized is ExpandoObject and it would need to be dynamic for that to work.

    Probably the best you can do is something like this:

    var catList = new[] { 
        new { name = "toto", age = 5 },
        new { name = "titi", age = 7 },
        new { name = "tata", age = 3 },
        new { name = "tutu", age = 4 } 
    }.Select(x => {
        dynamic d = new ExpandoObject();
        d.name = x.name;
        d.age = x.age;
        return d;
    }).ToList();
    
    Personally I'd probably just use a non-anonymous type...

    Monday, June 8, 2015 10:18 AM
    Moderator
  • Thank you Mike. There is no "trick" to make ExpandoObject dynamic ?

    Christopher84, what was your proposal when saying "You just add ExpandoObject to all the proper places in the list." ?


    • Edited by jmclej Monday, June 8, 2015 1:52 PM
    Monday, June 8, 2015 11:44 AM
  • "There is no "trick" to make ExpandoObject dynamic"

    Well, you make it dynamic by casting to dynamic or assigning to a variable of type dynamic like you do in your own example. But you can't use the initialization syntax and that's what you seem to be looking for.

    Monday, June 8, 2015 2:55 PM
    Moderator