I have a listItems
which I want to assign to a string. SO I tried like below
for (int i = 0; i < LocationDetails.Count; i++)
{
strMaintZone = String.Join(",", LocationDetails[i].LocationID);
}
But it is not getting assigned and it takes the last value. How do I assign the value to the string.
LINQ can be helpful here. You should pass IEnumerable<string>
to string.Join
method. The IEnumerable<string>
can be made by LINQ Select
clause.
strMaintZone = string.Join(",", LocationDetails.Select(item => item.LocationID));
In your example problem is that you every time override the value of strMaintZone
, and as a result you will have only the last element.