Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of object, get property with separator

Tags:

c#

list

public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

I have a list :

List<Person> list = new List<Person>();

I'd like get, the Id value of all entries of the list with comma separator, like this : id1, id2, id3

like image 663
Kris-I Avatar asked Dec 16 '22 21:12

Kris-I


1 Answers

Use string.Join to join values and Enumerable.Select to selected desired values:

string allIds = string.Join(", ", list.Select(i => i.Id.ToString()));
like image 114
Zbigniew Avatar answered Jan 01 '23 06:01

Zbigniew