I'm currently creating a anonymous object like this, it works fine so far.
var users = new
{
action = "users",
newbies = new[]
{
new
{
email = "abc",
username = "def",
location = "ghi"
}
}
}
Now I need to modify this, by adding more properties to the inner object.
I have a List that has users in it, which represents the users friends.
List<User> friends = ...
So the inner object should look something like:
new
{
email = "abc",
username = "def",
location = "ghi",
friend1email = "[email protected]",
friend2username = "f1",
friend2email = "[email protected]",
friend2username = "f2",
}
except I would need to get this using the LIst, so looping like:
foreach(var user in friends)
{
}
How can I generate this type of a anonymous object when I need to loop?
Note: I can't change the format here, I know it is silly but that is how it is.
Update
How can I do this using the ExpandoObject?
friendXemailproperties based on how many friends the associated list contains?DictionaryExpandoObjectdoes not have true "properties" - it just relies ondynamicto resolve them at run time. So if you have code that expect properties (like mapping or serialization) it may not work,