In C#, is there an "easy" way to perform string.Join on complex type list?

Viewed 13924

Let's say I have this object:

public class Role {
    public string Name { get; set; }
    public string Slug { get; set; }
    public DateTime DateAssigned { get; set; }
    ...
}

A member can have multiple roles: member.Roles = List<Role>();

If I wanted to join the member's roles into a comma separated list of the role names, is there an easy way (similar to string.Join(",", member.Roles); - which doesn't work because a role is a complex type)?

5 Answers

If you want multiple properties to be selected in the comma separated values:

var csvdata = member.Roles.Select(x => string.Join(",", new string[] { x.Name, x.Slug , x.DateAssigned}));
               

I had this requirement to select multiple properties to csv. May be this might help someone.

Related