0

Hi having array of ints like ages int[]{123, 234}

how to select it to string which can be used as get request ages=123&ages=234

1
  • ages constant in request must the same of the variable name or i can use constant like ages? Commented Apr 16, 2020 at 9:21

2 Answers 2

3

You can use a combination of string.Join and Select:

string.Join("&", ages.Select(age => $"ages={age}"));

Sign up to request clarification or add additional context in comments.

Comments

1

To do proper URL encoding, you can use HttpUtility.ParseQueryString() like this:

var query = System.Web.HttpUtility.ParseQueryString(string.Empty);

foreach (var age in ages)
{
    query.Add("ages", age.ToString());
}

// or: ages.ToList().ForEach(age => query.Add("ages", age.ToString()));

return query.ToString();

However, this will lead to ages=123,234.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.