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
}
VulpesPosted Mar 12, 2013, 6:02 AM
string str = String.Join(",", SalesPlan.Riders);
otherwise try:
string str = String.Join(",", SalesPlan.Riders.Select(r => r.ToString()).ToArray());
Muhammed AbdussalamPosted Mar 12, 2013, 4:35 AM
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
Vishal GilbilePosted Mar 12, 2013, 3:20 AM
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