Link to home
Start Free TrialLog in
Avatar of WorknHardr
WorknHardr

asked on

MVC 4 Linq Group in Two Column View?

I currently have one column showing in the View. I would like to have two or more columns side-by-side and grouped by Date as shown below.

09/2013                         10/2013
Food         $300.00         Food       $320.00
Housing   $600.00         Housing  $600.00
Utility       $200.00         Utility      $200.00

[View]
       <table>
                <thead>
                    <tr><td>09/2013</td></tr>
                    <tr>
                        <td><b>Category</b></td>
                        <td></td>
                        <td><b>Limit</b></td>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var group in Model)
                    {
                        <tr>
                            <td>@group.category.Name</td>
                            <td></td>
                            <td>@group.budget.Limit.ToString("c")</td>
                        </tr>
                    }
                </tbody>
            </table>

[Action]
        public ActionResult Index()
        {            
            var budgets = from b in context.Budgets
                          join c in context.Categories on b.CategoryID equals c.CategoryID
                          select new BudgetViewModel { category = c, budget = b };

            return View(budgets.ToList());
        }

Open in new window

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

What is your question?

How to create a group in LINQ?

How to modify the view to show the output?
Avatar of WorknHardr
WorknHardr

ASKER

I don't know how to group the rows into columns by date (month/year).

It will be both Linq and Html coding...
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I took more than a Simple Group by Expression. Thanks for making me rethink the Group clause. My final Linq is below.

var budgets = (from b in context.Budgets
                          join c in context.Categories on b.CategoryID equals c.CategoryID
                          let j = new BudgetViewModel { category = c, budget = b }
                          group j by j.budget.Date into g
                          select new Group<DateTime, BudgetViewModel>
                                { Key = g.Key, Values = g.OrderBy(s => s.category.Name) });