-2

I have the following for loop written in PHP:

$activesFilters = 0;

foreach ($_GET as $key => $value) {

    if(!empty($value)) $activesFilters++;

}

if(isset($_GET['Filter'])) { $activesFilters = $activesFilters - 1; }

if(isset($_GET['CustomFilter'])) { $activesFilters = $activesFilters - 1; }

if(isset($_GET['showArchived'])) { $activesFilters = $activesFilters - 1; }

and I want to convert it over to ASP.NET C# Razor.

So far I have:

int activeFilter = 0;

@foreach(Request.QueryString as key in value) {
   if(!empty(value)) activeFilters+1;
}

@if(Request.QueryString['Filter']) activeFilters = activeFilters - 1;

@if(Request.QueryString['CustomFilter']) activeFilters = activeFilters - 1;

@if(Request.QueryString['showArchived']) activeFilters = activeFilters - 1;

It's the foreach loop I'm confused about.

As I don't understand how to handle the key => value in C#

I've written the above based on: http://msdn.microsoft.com/en-us/library/ttw7t8t6(v=vs.90).aspx but it doesn't show any examples for handling the key + value part.

UPDATE:

Based on some help from below:

@int activeFilters = 0;

@foreach (string key in Request.QueryString)
{
    string value = Request.QueryString[key];

    if(String.IsNullOrEmpty(value)){
        return activeFilters+1;
    }

}

However it says activeFilters does not exist in the current context. How do I expose it to the foreach loop in C#? As this is fine in the PHP world.

3
  • So, what is the issue that you are having? Commented Nov 11, 2013 at 18:25
  • 3
    I have voted to close because this question demonstrates no effort in solving. A simple search would immediatly tell you how a foreach loop works in C#. Commented Nov 11, 2013 at 18:27
  • 1
    @JeroenVannevel I did search to get to the code I have above in the question. Otherwise I wouldn't have anything to show. Not understanding how something works does not mean it demonstrates lack of effort! And assumptions that someone would 'immediately' understand something based on an article is condescending! Commented Nov 11, 2013 at 18:49

1 Answer 1

2
foreach (string key in Request.QueryString)
{
    string value = Request.QueryString[key];
    // add your other code here
}

Now, you iterate over all keys in the query string, and then you get their values using Request.QueryString[key].

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

5 Comments

Thanks for that. If you take a look at the code above. You can see what additional problem I have encountered with regards to the count of the values.
@Cameron: Just write activeFilters++ or activeFilters = activeFilters + 1
Yeah tried that. But it says activeFilters doesn't exist. Even though I have declared it above the foreach loop?
@Cameron: Please see this SO question: stackoverflow.com/q/6601715/2619912
Oh WOW! C# is incredibly strict! :P Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.