Razor 页面上 @foreach 的正确语法,其中值可以等于多个值

问题描述 投票:0回答:1

我需要帮助以确保我的 razor 页面上此代码的语法正确:

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

基本上,我想按报告的此特定部分中的特定子集

InvestType
值进行过滤。我不希望包含
InvestType
的其他值。我不知道如何做“in”类型的陈述。

感谢您提供的任何帮助。

asp.net arraylist foreach razor-pages
1个回答
0
投票

您可以使用

@{ ... }
.cshtml
文件中声明变量:

@{
    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))
{

}

或者您可以将其添加到您的

PageModel
中,如下所示:

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

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

并在

.cshtml

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

}
© www.soinside.com 2019 - 2024. All rights reserved.