2

I need some help with grouping my data. I have the following bit below, where data is displayed based on a foreach loop.

Very straight forward stuff, but I need to group it by item.PARTY. There are ever only two and when you click on one it expands ( I can use the bootstrap bit for that). Its just the grouping I'm a bit confused and not even sure where to start and what is best practise.

 @foreach (var item in Model.FinplanExpenseView)
    {
        <tr>
            <td class="text-center">
                @if (item.VIEW_TYPE == "Budget")
                {                   
                     @Html.ActionLink("X", "Delete", item.VIEW_TYPE, new { id = item.FINPLAN2_GID, ReturnAction = "IncomeAndExpenses" }, null)
                }
                else if (item.VIEW_TYPE == "LivingExpenses")
                {
                    @Html.ActionLink("X", "Delete", item.VIEW_TYPE, new { id = item.FINPLAN2_GID, party = item.PARTY, ReturnAction = "IncomeAndExpenses" }, null)                   
                }
                else
                { 
                    @Html.ActionLink("X", "Delete", item.VIEW_TYPE, new { id = item.SOURCE_GID, ReturnAction = "IncomeAndExpenses" }, null)                   
                }
            </td>
            <td>
                @if (item.VIEW_TYPE == "Budget")
                {                   
                    @item.DESC
                }
                else if (item.VIEW_TYPE == "LivingExpenses")
                {                   
                    @item.DESC
                }
                else
                {                   
                    @item.DESC
                }

            </td>           
            <td class="numeric-column">R @Html.DisplayFor(modelItem => item.AMOUNT)</td>
            <td class="text-right">@Html.DisplayFor(modelItem => item.PARTY)</td>
            <td class="text-right">                
                @if (item.VIEW_TYPE == "Budget")
                {                   
                     @Html.ActionLink("edit", "Edit", item.VIEW_TYPE, new { id = item.FINPLAN2_GID, ReturnAction = "IncomeAndExpenses" }, null)
                }
                else if (item.VIEW_TYPE == "LivingExpenses")
                {
                    @Html.ActionLink("edit", "Edit", item.VIEW_TYPE, new { id = item.FINPLAN2_GID, party = item.PARTY, ReturnAction = "IncomeAndExpenses" }, null)                   
                }
                else
                { 
                    @Html.ActionLink("edit", "Edit", item.VIEW_TYPE, new { id = item.SOURCE_GID, ReturnAction = "IncomeAndExpenses" }, null)                   
                }
            </td>
        </tr>
    }

Any help would be appreciated!

anonymous
  • 85
  • 1
  • 11
  • What is the type of item.Party? A string? And when you say "there are only ever two", do you mean only two values? Many items would then be able to have those values, right? – jamesSampica Aug 18 '14 at 13:20

1 Answers1

1

You can use an orderby for this. You want all the Party items after each other right?

Change this: @foreach (var item in Model.FinplanExpenseView)

in

@foreach (var item in Model.FinplanExpenseView.Orderby(f => f.PARTY)

LockTar
  • 5,364
  • 3
  • 46
  • 72
  • Hi excellent thanks yes, the grouping is working but how do i restart the group header so it has a title e.g. PARTY 1 is a th and then its values and then PARTY 2 as a header and it's values – anonymous Aug 18 '14 at 13:40
  • If you want that. You can better create a linq group. Than you can create a foreach for every group like PARTY1, PARTY2 etc and in every group, you could create a foreach for every item in that PARTY. Because you create multiple foreaches nested, you can create a separate header for each PARTY. See this example: http://stackoverflow.com/a/7325306/801005 – LockTar Aug 18 '14 at 15:23