-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.

Cameron
  • 27,963
  • 100
  • 281
  • 483
  • So, what is the issue that you are having? – RealityDysfunction Nov 11 '13 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#. – Jeroen Vannevel Nov 11 '13 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! – Cameron Nov 11 '13 at 18:49

1 Answers1

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].

ProgramFOX
  • 6,131
  • 11
  • 45
  • 51