【问题标题】:How can I make C# foreach with nested subtitles?如何使用嵌套字幕制作 C# foreach?
【发布时间】:2017-02-24 12:23:07
【问题描述】:

您好,我是一名初学者,我正在使用 ASP.NET 和 MVC5

我想在 1 个 foreach 中创建一个带有嵌套标题(课程)的 IEnumerable Food-Menu-Pricelist,这样我就可以使用 CRUD-Operations I 创建或编辑一个新字符串(CourseName)用实体框架做的。

我将在下面的链接中嵌入一张图片,它是我进行半/硬编码的,就像您在下面的代码中看到的那样标题是硬编码的。

我想要达到的结果预览:

我有 2 个模型:

  1. Course.cs = 带有属性字符串的标题:CourseName
  2. Dish.cs = 以及带有属性字符串的列表:DishName

有没有一种方法可以将这个表放在 1 个 foreach 中,这样我也可以使用我定义的模型使用 CRUD-Operations 编辑标题?

我已经尝试了很多来达到这个目标。无论如何,这是我在 Stackoverflow 上的第一个问题..

    @model IEnumerable<Project_Lameau.Models.Dish>

@{
    ViewBag.Title = "Index";
}

<h2>Suggestie</h2>

<table class="table">
    @foreach (var item in Model.Where(d => d.CourseName.Contains("Suggestie")))
    {
        <tr>
            <td>@item.DishName</td>
            <td class="text-right">@item.Price</td>
        </tr>
    }
</table>
<hr />


<h2>Salade</h2>

<table class="table">
    @foreach (var item in Model.Where(d => d.CourseName.Contains("Salade")))
    {
        <tr>
            <td>@item.DishName</td>
            <td class="text-right">@item.Price</td>
        </tr>
    }
</table>
<hr />


<h2>Voorgerecht</h2>

<table class="table">
    @foreach (var item in Model.Where(d => d.CourseName.Contains("Voorgerecht")))
    {
        <tr>
            <td>@item.DishName</td>
            <td class="text-right">@item.Price</td>
        </tr>
    }
</table>
<hr />


<h2>Hoofd Vlees</h2>

<table class="table">
    @foreach (var item in Model.Where(d => d.CourseName.Contains("Hoofd Vlees")))
    {
        <tr>
            <td>@item.DishName</td>
            <td class="text-right">@item.Price</td>
        </tr>
    }
</table>
<hr />


<h2>Hoofd Vis</h2>

<table class="table">
    @foreach (var item in Model.Where(d => d.CourseName.Contains("Hoofd Vis")))
    {
        <tr>
            <td>@item.DishName</td>
            <td class="text-right">@item.Price</td>
        </tr>
    }
</table>
<hr />


<h2>Vega</h2>

<table class="table">
    @foreach (var item in Model.Where(d => d.CourseName.Contains("Vega")))
    {
        <tr>
            <td>@item.DishName</td>
            <td class="text-right">@item.Price</td>
        </tr>
    }
</table>

【问题讨论】:

  • Dish 类是什么样的?
  • public class Dish { public int Id { get;放; } 公共 int CourseId { 获取;放; } 公共字符串 DishName { 获取;放; } 公共小数?价格{得到;放; } 公共课程课程 { 获取;放; } }

标签: c# linq foreach


【解决方案1】:

感谢罗伯的帮助。我很感激! 我选择第一个选项。 我必须使用 lamda 表达式 .Include(m =&gt; m.Course) 扩展控制器代码,因为我需要包含第一类 (Course.cs) 的属性 (CourseName)。 Dish 类已经有了 Lookup 属性 public Course Course { get; set; }) 所以我在 @foreach (var courseGroup in Model.GroupBy(m =&gt; m.Course.CourseName)) 中更改了顶层 foreach,然后一切正常。

第二个选项不起作用。我认为是因为两个类 Course.cs 和具有 CourseId 属性的 Dish.cs 之间相关的演示数据。

干杯!

【讨论】:

    【解决方案2】:

    您的顶级foreach 将类似于

    foreach (var courseGroup in Model.GroupBy(m => m.CourseName))
    

    每个项目将是:

    <h2>courseGroup.Key</h2>
    
    <table class="table">
        @foreach (var item in courseGroup)
        {
            <tr>
                <td>@item.DishName</td>
                <td class="text-right">@item.Price</td>
            </tr>
        }
    </table>
    

    但是,您可以进一步组织数据,因此如果您定义类(和数据库中的表),如下所示:

    public class Course
    {
        public string CourseName { get; set; }
        public List<Dish> Dishes { get; set; }
    }
    
    public class Dish
    {
        public string DishName { get; set; }
        public decimal Price { get; set; }
    }
    

    那么你的代码就变得简单多了:

    foreach (var course in Model) //or Model.Courses - up to you
    

    每个项目将是:

    <h2>course.CourseName</h2>
    
    <table class="table">
        @foreach (var item in course.Dishes)
        {
            <tr>
                <td>@item.DishName</td>
                <td class="text-right">@item.Price</td>
            </tr>
        }
    </table>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-12
      • 1970-01-01
      • 2019-08-05
      • 2018-01-21
      • 1970-01-01
      • 2021-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多