Hi,
I have a LINQ collection.I want to take all values from collection and set into string variable.
My scenario is-->
Foreach(var riderId in SalesPlan.Riders)
{
string str= 101,102,103; //This is riderId coming from salePlan.riders
// This str will fill from collection, var riderId
}

VulpesRank #55 · 29.2K reputationPosted Mar 12, 2013, 2:02 AM EDT
string str = String.Join(",", SalesPlan.Riders);
otherwise try:
string str = String.Join(",", SalesPlan.Riders.Select(r => r.ToString()).ToArray());
Vishal GilbileRank #268 · 9.4K reputationPosted Mar 11, 2013, 11:20 PM EDT
It's quite simple instead of declaring a string variable inside foreach loop try to declare it outside foreach loop. So that the value will not be replaced because as an when your loop moves a new string str variable is created as per your code.
Try following
string str="";
Foreach(var riderId in SalesPlan.Riders)
{
str+= riderId+",";
}
Hope that solves your problem.
With Regards,
Vishal Gilbile
Muhammed Abdussalam63 reputationPosted Mar 12, 2013, 12:35 AM EDT
I have one suggestion on this, Please have a look at the code.Given a Sample Console App Code.. Please let me know if further assistance needed.
Regards
Salam