Correct syntax for @foreach on Razor page where value can be equal to more than one thing

35 Views Asked by At

I need assistance with getting the syntax correct for this code on my razor page:

@foreach (var officerGroup in Model.Results
                                   .Where(i => i.InvestType in ("YO", "CRD", "PPI", "PSI")
                                   .GroupBy(x => x.ProbOfficer))

Basically, I want to filter by a specific subset of InvestType values in this particular section of a report. There are other values for InvestType that I DON'T wish to include. I don't know how to do an "in" type of statement.

Thanks for any help you can provide.

1

There are 1 best solutions below

1
Dimitris Maragkos On BEST ANSWER

You can use @{ ... } to declare variables inside a .cshtml file:

@{
    var fullReportList = new List<string> { "CRD", "PPI", "PSI", "Supvsn Only", "TO" };
}

@foreach (var officerGroup in Model.Results
                                   .Where(i => fullReportList.Contains(i.InvestType))
                                   .GroupBy(x => x.ProbOfficer))
{

}

Or you can add it in your PageModel like this:

public class DemoModel : PageModel
{
    public List<string> FullReportList => new() { "CRD", "PPI", "PSI", "Supvsn Only", "TO" };

    public void OnGet()
    {
        //...
    }
}

and in the .cshtml:

@foreach (var officerGroup in Model.Results
                                   .Where(i => Model.FullReportList.Contains(i.InvestType))
                                   .GroupBy(x => x.ProbOfficer))
{

}