Link to home
Start Free TrialLog in
Avatar of Scarlett72
Scarlett72

asked on

How to use string.join in method

I'm trying to create a method that returns a comma separated list of all Calendar controls on a page, the solutions I've read have suggested using string.join; however, I an confused on how to implement this.  Could this also be an alternative solution?

protected String GetAllCalControls()
    {
        StringBuilder countCal = new StringBuilder();            
        foreach (Control ctrl in Page.Controls)
        {
            foreach (Control childc in ctrl.Controls)
            {
                if (childc is Calendar)
                {
                    countCal.Append(childc.ID+",");
                }
            }
        }
        countCal.Remove(countCal.Length, -1);
        return countCal.ToString();
    }

Open in new window

Avatar of it_saige
it_saige
Flag of United States of America image

You might be looking for something like:
private string GetAllControls()
{
	return string.Join(",", (from Control control in Controls where control is Calendar select (control as Calendar).ID).ToArray());
}

Open in new window


Make sure you add using System.Linq if it is not already there.

-saige-
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Scarlett72
Scarlett72

ASKER

Thank you it_saige, this is fantastic.  I really need to play with it some more and read up on the concepts you've used to fully understand your solution.