Concatenate values in a List with LINQ

I had a stored procedure that accepted a comma delimited string of id’s as a parameter. My source was a generic list of "Employee" objects from which I had to get the id’s and create a comma delimited string.

image

To accomplish this I used a LINQ Aggregate Query:

  1. Dim employees As New List(Of Employee)
  2.  
  3. Dim e1 As New Employee() With { _
  4.     .EmployeeId = Guid.NewGuid, _
  5.     .FirstName = "John", _
  6.     .LastName = "Smith", _
  7.     .Position = "Engineer"}
  8.  
  9. Dim e2 As New Employee() With { _
  10.     .EmployeeId = Guid.NewGuid, _
  11.     .FirstName = "Bob", _
  12.     .LastName = "Johnson", _
  13.     .Position = "Business Analyst"}
  14.  
  15. employees.Add(New Employee() with
  16.  
  17. Dim employeeIds As String = _
  18.         employees.Select(Function(x) x.EmployeeId.ToString()) _
  19.                     .Aggregate(Function(y, z) y & "," & z)

In the example above, I used a comma as my separator.