按属性分组记录将放在表

时间:2017-03-03 21:04:21

标签: html asp.net-mvc razor

如果我有一个像这样的对象列表:

ID    |    First Name    |    Location    |    Division    |    Age    |
------------------------------------------------------------------
 1          John              Building1         ISS              22
 2          Alex              Building1         ISS              30
 3          Chris             Building1         HR               41
 4          Mary              Building1         HR               25
 5          Monica            Building1         HR               46
 6          Ian               Building2         Accounting       37
 7          John              Building2         Accounting       32
 8          Justin            Building2         Sales            22
 9          Trevor            Building2         Sales            26
10          Shelley           Building2         Sales            34
11          Troy              Building1         Network          25

这只是我所拥有的样本......我有很多

我的目标是每次创建一个表格,如果位置不同 OR 该部门不同。

所以我希望我的结果是:

表1

Building 1 - ISS // table header
------------------------------------------------------------------
// table body
 John              Building1         ISS              22
 Alex              Building1         ISS              30

表2

Building 1 - HR// table header
------------------------------------------------------------------
// table body
 Chris              Building1         HR              41
 Mary               Building1         HR              25
 Monica             Building1         HR              46

表3

Building 2 - Accounting // table header
------------------------------------------------------------------
// table body
 Ian              Building2         Accounting              37
 John             Building2         Accounting              32

表4

Building 2 - Sales // table header
------------------------------------------------------------------
// table body
 Justin              Building2         Sales              22
 Trevor              Building2         Sales              26
 Shelley             Building2         Sales              34

在我看来,我有:

@model List<ProjectName.Models.ViewModels.DetailsViewModel>

我想只使用一个foreach循环,因为就像我上面说的那样,有更多的位置和分区,我宁愿不必为每个不同的位置和分区创建一个foreach循环。

如何做到这一点?

感谢任何帮助。

更新

我在上表中添加了一个额外的内容。那个人是Building1位置的一部分,但由于他只有一个,而不是仅为一个人生成一个整表,我想将他添加到Building1 ISS组。

我可以做某种条件陈述吗?

更新2

ID    |    First Name    |    Location    |    Division    |    Age    |
------------------------------------------------------------------
 1           John             Building1     Administration      40
 2           Melissa          Building1     Division1           32
 3           Sean             Building1     Division1           26
 4           Timothy          Building1     Division1           33
 5           Joe              Headquarters  Administration      33
 5           Jason            Headquarters  Staff               33
 5           Greg             Headquarters  Staff               33
 5           Brett            Headquarters  Staff               33
 1           Rob              Building2     Administration      40
 2           Nate             Building2     Division2           32
 3           Leslie           Building2     Division2           26
 4           Phil             Building2     Division2           33

目标

我不关心总部的人......如果一个部门只有一个人那么就这样吧......我只关心建筑1和建筑2人。

所以我希望Rob加入到Building2 Division2小组,John加入了Building1 Division1小组。

这里可以使用条件语句来检查count和where语句来检查位置名称吗?

1 个答案:

答案 0 :(得分:1)

您需要使用.GroupByLocation上的Division作为密钥查询记录。

从代表您视图的视图模型开始

public class MainVM
{
    public string Title { get; set; }
    public IEnumerable<ChildVM> Children { get; set; }
}
public class ChildVM
{
    public string FirstName { get; set; }
    public string Location { get; set; }
    public string Division { get; set; }
    public int Age { get; set; }
}

生成模型

var data = ... // your query to get the database records

IEnumerable<MainVM> model = data.GroupBy(x => new { x.Location, x.Division }).Select(y => new MainVM()
{
    Title = y.Key.Location + " - " + y.Key.Division,
    Children = y.Select(z => new ChildVM()
    {
        FirstName = z.FirstName,
        Location = z.Location,
        Division = z.Division,
        Age = z.Age
    })
});
return View(model);

并在视图中

@model IEnumerable<MainVM>
....
@foreach(var table in Model)
{
    <table>
        <thead><tr><th colspan="4">@table.Title</th></tr></thead>
        <tbody>
            @foreach(var row in table.Children)
            {
                <tr>
                    <td>@row.FirstName</td>
                    .... // do you really need to repeat Location and Division which are already in the header?
                    <td>@row.Age</td>
                </tr>
            }
        </tbody>
    </table>
}

根据您更新的问题,您需要选择ChildVM .Count() MainVM.Children == 1的所有MainVM记录,然后将其连接到第一个IEnumerable<MainVM> model = data.GroupBy(... // as above model.First().Children = model.First().Children .Concat(q.Where(x => x.Children.Count() == 1).SelectMany(x => x.Children));

MainVM

然后只返回.Children.Count > 1

model = model.Where(x => x.Children.Count() > 1); return View(model);
{{1}}