C# list to anonymous type with new properties

This is my expected anonymous type out of a list object;

var roleData = new[] {
    new { roleName = "Office Director"},
    new { roleName = "Manager Operations"},
    new { roleName = "Training Manager" }
};

This is how we can create anonymous type out of a strongly typed C# list;

foreach (UserLocation location in request.RoleRequest.UserLocations)
{
   ....................
   var rolesData = location.Roles
               .Select(x => new { roleName = x.RoleName }).ToArray();  
}

For more examples, continue on Stack Overflow

FavoriteLoadingAdd to favorites
Spread the love

Author: Shahzad Khan

Software developer / Architect