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!

1
  • 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? Commented Aug 18, 2014 at 13:20

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

2 Comments

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
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: stackoverflow.com/a/7325306/801005

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.