MVC:视图显示分组列表

时间:2019-06-09 15:41:12

标签: c# model-view-controller view

我正在Asp.net Framework上使用MVC 5,并希望创建一个对信息进行分组的视图,如下所示:

Animal Species: Lion
      Individual: Mufusa, age 6
      Individual: Simba, age 2
Animal Species: Wolf
      Individual: Lupa, age 5
      Individual: Charlie, age 3

现在,我在View文件中使用了“ @foreach(模型中的变量项)”语句,输出如下所示:

Animal Species: Lion
     Individual: Mufusa, age 6
Animal Species: Lion
     Individual: Simba, age 2
Animal Species: Wolf
     Individual: Lupa, age 5
Animal Species: Wolf
     Individual: Charlie, age 3

关于如何执行此操作的任何建议?预先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

     var queryGroupBySpecies = from animal in animals
                                   group new { animal .FirstName, animal.age}  75 into animalGroup 
select animalGroup ;

然后您将遍历两次

   foreach (var animalGroup in queryGroupBySpecies )
    { 
        foreach (var animal in animalGroup )
            Console.WriteLine($"\t{animal.FirstName} {animal.age}");
    }