如何根据属性将对象分组到asp.net mvc视图中的表中

时间:2016-01-04 00:34:48

标签: c# asp.net-mvc

我是ASP.NET MVC的新手,我有一个传递给视图的对象列表,我需要根据它们的两个属性将它们分组到一个表中。例如,每个对象都具有以下属性:

public class Student
{
    public int id;
    public string name;
    public int gpa;
    public string school;
    ...
}

在我看来,我希望有一个表格,其中列是不同的gpa范围,每行都是一个学校。在表格的每个单元格中,我想显示属于特定gpa范围的特定学校的所有学生的姓名。例如:

模特中的学生名单:

Student{id=1, name="Alice",   gpa=3.5, school="School A"};
Student{id=2, name="Bob",     gpa=2.5, school="School A"};
Student{id=3, name="Charlie", gpa=1.5, school="School A"};
Student{id=4, name="David",   gpa=2.5, school="School B"};
Student{id=5, name="Ellen",   gpa=1.0, school="School B"};
Student{id=6, name="Frank",   gpa=2.5, school="School C"};
Student{id=7, name="Garry",   gpa=3.5, school="School C"};

结果表应该在视图中看起来像这样:

                  [0.0, 2.0)          [2.0, 4.0]
----------------------------------------------------------
School A          Charlie             Alice
                                      Bob
----------------------------------------------------------
School B          Ellen               David
----------------------------------------------------------
School C                              Frank
                                      Garry
----------------------------------------------------------

在将未分类对象列表传递到视图中时,是否有一种简单的方法可以实现此目的?这似乎是一个问题,可以出现很多,但我找不到任何现有的解决方案。有人可以帮我吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

您想要执行GROUP BY并且可以使用LINQ查询执行此操作。

var result =
    from student in studentList
    group student by new {student.school, student.gpa} into newGroup
    select newGroup;

根据您的评论进行修改:

  

如何获得具体价值,例如{"学校A",2.0}?

您可以更改查询

        List<Student> students = new List<Student>()
        {
            new Student{id=1, name="Alice",   gpa=3.5, school="School A"},
            new Student{id=2, name="Bob",     gpa=2.5, school="School A"},
            new Student{id=3, name="Charlie", gpa=1.5, school="School A"},
            new Student{id=4, name="David",   gpa=2.5, school="School B"},
            new Student{id=5, name="Ellen",   gpa=1.0, school="School B"},
            new Student{id=6, name="Frank",   gpa=2.5, school="School C"},
            new Student{id=7, name="Garry",   gpa=3.5, school="School C"},
        };


        var result = from student in students
                     group student by new { student.school, student.gpa } into newGroup
                     select new 
                      {   Gpa = newGroup.Key.gpa, 
                          School = newGroup.Key.school, 
                          StudentName = newGroup.Select(x => x.name) 
                      };

您现在可以使用WHERE条件,例如

result.Where(a => a.School == "School A" && a.Gpa == 2.5)
  

我如何使用&#34;结果&#34;在这里填写我视图中的表格?

如果您想将此作为模型传递给视图,那么最好创建一个视图模型并将其传递给查看并创建该类型的强类型视图,如

您的viewmodel

public class StudentView
{
    public IEnumerable<string> StudentName { get; set; }
    public double Gpa { get; set; }
    public string School { get; set; }
}

将查询更改为

        IEnumerable<StudentView> result = from student in students
                     group student by new { student.school, student.gpa } into newGroup
                     select new StudentView 
                      {   Gpa = newGroup.Key.gpa, 
                          School = newGroup.Key.school, 
                          StudentName = newGroup.Select(x => x.name) 
                      };

现在将其传递给您的观点

return View(result);