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.
To accomplish this I used a LINQ Aggregate Query:
- Dim employees As New List(Of Employee)
- Dim e1 As New Employee() With { _
- .EmployeeId = Guid.NewGuid, _
- .FirstName = "John", _
- .LastName = "Smith", _
- .Position = "Engineer"}
- Dim e2 As New Employee() With { _
- .EmployeeId = Guid.NewGuid, _
- .FirstName = "Bob", _
- .LastName = "Johnson", _
- .Position = "Business Analyst"}
- employees.Add(New Employee() with
- Dim employeeIds As String = _
- employees.Select(Function(x) x.EmployeeId.ToString()) _
- .Aggregate(Function(y, z) y & "," & z)
In the example above, I used a comma as my separator.