1

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

2
  • 1
    I think you'd need to write logic to assemble the groups yourself - there's LINQ to group by customer but I can't think of anything to turn a set of numbers into a string describing the range they cover (barring simple min / max, ignoring whether the range is covered or not) Commented Aug 31, 2011 at 10:09
  • Just like the good link: msdn.microsoft.com/en-us/vcsharp/aa336746 Hereyou can find good examples of linq using Commented Aug 31, 2011 at 10:25

1 Answer 1

3

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.