Brian Hicks
Brian Hicks

Reputation: 6413

Iterate over all Query Values in C#

I'm trying to iterate over an unknown number of query values in C#... and can't find anything unrelated to LINQ, which I can't use. Anyone have any ideas?

Upvotes: 2

Views: 7936

Answers (3)

Charles
Charles

Reputation: 756

Using the Request.QueryString gives you a collection that you can iterate over. Using Request.QueryString.Allkeys allows you to iterate over a collection of strings that represent all of the keys i nthe query string. Using this we can come up with something like the below code in order to iterate over all keys and get their values.

foreach (string key in Request.QueryString.AllKeys)
{
    Response.Write("Key: " + key + " Value: " + Request.QueryString[key]);
}

Hope this helped.

Upvotes: 9

rball
rball

Reputation: 6955

If the collection implements IEnumerable you can use a foreach, otherwise use a for loop with the .Length of the collection.

Upvotes: 0

Sean
Sean

Reputation: 599

If this question is about getting a querystring in ASP.NET, I think the link you are searching for is:

http://msdn.microsoft.com/en-us/library/system.web.httprequest.querystring.aspx

Essentially, Request.QueryString gives you a collection that you can then iterate over.

Upvotes: 3

Related Questions