You've declared the
categoryName
list
outside of your loop. Every row is bound to the same list of strings, and you add the categories for all rows to that one list. Since you clear the list at the start of each iteration of the loop, the list will only contain the categories for the last row.
Move the list variable inside your loop, and each row will get its own separate list:
foreach (var item in list)
{
List<String> categoryName = new List<string>();
...
NB: You're doing a lot of expensive reflection within your loop. There's almost certainly a better approach. At the very least, you should cache the
item.GetType()
call in a variable, and cache the value of any properties you read more than once:
foreach (var item in list)
{
Type itemType = item.GetType();
Guid id = (Guid)itemType.GetProperty("id").GetValue(item, null);
...