No, even anonymous types must have compile-time field names. It seems like a collection of different types is required, each with different field names. Maybe you could use Dictionaryinstead?
var result = linqDoc.Descendants()
.GroupBy(elem => elem.Name)
.ToDictionary(
g => g.Key.ToString(),
g => g.Attributes("Id").Select(attr => attr.Value).ToList()
);
Note that dictionaries can be easily serialized in JSON:
{
"key1": "type1":
{
"prop1a":"value1a",
"prop1b":"value1b"
},
"key2": "type2":
{
"prop2a":"value2a",
"prop2b":"value2b"
}
}
source
share