实体框架 MVC 中的分组

发布于 2024-12-02 06:53:52 字数 913 浏览 0 评论 0原文

大家好,我目前已经使用以下方式输出了电话号码列表:-

控制器:-

public ActionResult ViewSubRange(int id)
{
    IEnumerable<Number> numbers = context.Numbers.Where(m => m.RangeID == id).ToList();

    return View("SubRange", numbers);
}

视图:-

@model IEnumerable<TelephoneNumberManagement.Models.Number>
<table>
    <tr>
        <th>
            Number
        </th>
        <th>
            Status
        </th>
    </tr>

@foreach (var item in Model)
{
    <tr> 
        <td>@item.Number1</td>
        <td>@item.Status.StatusName</td>
    </tr> 

}

</table>

这很好,但是我注意到我们可以输出很多号码。我想知道是否可以对数字进行分组,例如按客户分组。所以我想要实现的是:-

01132210000-01132210999 客户A

01132211000-01132211009 客户B

01132211010-01132211029 客户 C​​

Hi all i've currently got a list of telephone numbers being output using:-

Controller:-

public ActionResult ViewSubRange(int id)
{
    IEnumerable<Number> numbers = context.Numbers.Where(m => m.RangeID == id).ToList();

    return View("SubRange", numbers);
}

View:-

@model IEnumerable<TelephoneNumberManagement.Models.Number>
<table>
    <tr>
        <th>
            Number
        </th>
        <th>
            Status
        </th>
    </tr>

@foreach (var item in Model)
{
    <tr> 
        <td>@item.Number1</td>
        <td>@item.Status.StatusName</td>
    </tr> 

}

</table>

This is fine, however i've noticed that we can have a lot of numbers being output. I was wondering if its possible to group the numbers, so for example by Customer. So what i want to achieve is something like:-

01132210000-01132210999 CUSTOMER A

01132211000-01132211009 CUSTOMER B

01132211010-01132211029 CUSTOMER C

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泅人 2024-12-09 06:53:52

您可以定义一个新的视图模型:

public class MyViewModel
{
    public string StatusName { get; set; }
    public string Numbers { get; set; }
}

然后按客户名称分组:

public ActionResult ViewSubRange(int id) 
{
    var numbers = context.Numbers
        .Where(m => m.RangeID == id)
        .GroupBy(x => x.Status.StatusName)
        .Select(x => new MyViewModel
        {
            StatusName = x.Key,

            // TODO: could change the format if you will or
            // select the min and max or whatever you need
            Numbers = string.Join("-", x.Select(n => n.Number1)) 
        })
        .ToList();
    return View(numbers);
}

最后在您的视图中:

@model IEnumerable<MyViewModel>
<table>
    <tr>
        <th>Number</th>
        <th>Status</th>
    </tr>

    @foreach (var item in Model)
    {
        <tr> 
            <td>@item.Numbers</td>
            <td>@item.StatusName</td>
        </tr> 
    }
</table>

You could define a new view model:

public class MyViewModel
{
    public string StatusName { get; set; }
    public string Numbers { get; set; }
}

and then group by customer name:

public ActionResult ViewSubRange(int id) 
{
    var numbers = context.Numbers
        .Where(m => m.RangeID == id)
        .GroupBy(x => x.Status.StatusName)
        .Select(x => new MyViewModel
        {
            StatusName = x.Key,

            // TODO: could change the format if you will or
            // select the min and max or whatever you need
            Numbers = string.Join("-", x.Select(n => n.Number1)) 
        })
        .ToList();
    return View(numbers);
}

and finally in your view:

@model IEnumerable<MyViewModel>
<table>
    <tr>
        <th>Number</th>
        <th>Status</th>
    </tr>

    @foreach (var item in Model)
    {
        <tr> 
            <td>@item.Numbers</td>
            <td>@item.StatusName</td>
        </tr> 
    }
</table>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文