Join IDs from a list
c#
Solution
You can use:
string list = string.Join(", ", sessionProducts.Select(p => p.id));
This uses the generic version of string.Join, as well as `Enumerable.Select` to convert from your Product collection to an `IEnumerable<int>`.
Problem
The goal Join each id of all elements from a List into a concatenated string like `1, 2, 3`. The problem In my application there is a method whose its only parameter is of `string` type and I want to pass a value like `1, 2, 3, 4, 5` to it. Those values (`1, 2, 3, 4, 5`) will be sent to me by a `List<Product>` — I need to get the `id` of each element from this list. How can I get these `ids` and concatenate them into a single string? What I've already tried I've already tried the following code (based on this question): ``` List<Products> sessionProducts = sessionStore.Get("ProductsSummary"); // ProductsSummary is the index of my Session. string list = string.Join(", ", sessionProducts); return Content(list); ``` But without success. When I access my view, the return is: MyApp.Models.Data.Products Techincal details My .Net version is 4.5. Duplicated? I think it isn't because I searched on Google and SO, but without success. Moreover, what I found doesn't helped me.