2

Let's say I have a list:

var list = new List<int>{1,2,3};

How do I add this to an anonymous object such that the object would look like:

{ list : [1,2,3] }
user3912349
  • 163
  • 1
  • 8

2 Answers2

0
var obj = new 
{
    list = list
};

obj is now an anonymous object with a property called list of type List<int>.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

By creating the anonymous object:

var anon = new { list }

I assume that by saying you want it to look like this:

{ list : [1,2,3] }

you are talking about the output of a JSON serialization, in which it would look like once you serialize.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321