0

I am trying to return records from db and group by Transaction Date so I can see only one record for each specific date in the view if I have multiple transactions in the same day. I did the query and it is working fine but the view is not displaying the result and I am getting an error:

The model item passed into the dictionary is of type 'System.Collections.Generic.List1[System.Linq.IGrouping2[System.DateTime,TouchlessWeb.Models.ClientAccounting]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[TouchlessWeb.Models.ClientAccounting]'.

My controller Code:

var query = db.clientAccountings.Where(u => u.OrgId == id).GroupBy(o => o.TransDate);
                return View(query.ToList());

and My View:

@model IEnumerable<TouchlessWeb.Models.ClientAccounting>
@foreach (var user in Model)
            {
            <tbody>
                <tr>
                    <td>@user.TransDate </td>
                    <td>RM @user.Amount</td>
                    <td>
                        @if (user.AccType == 1)
                        {
                            <span class="text-danger">Seasonal</span>
                        }
                        @if (user.AccType == 2)
                        {
                            <span>Occasional</span>
                        }

                    </td>
                    <td>@user.TransID</td>
                    <td>Not Paid</td>
                    <td>@user.RefNo</td>


                </tr>
            </tbody>
            }

I am new to MVC, I hope you can help me. Thanks

Osama Shaki
  • 139
  • 15
  • 1
    You may also find this useful: https://stackoverflow.com/questions/7325278/group-by-in-linq – Tieson T. Feb 09 '18 at 03:54
  • Your `.GroupBy()` clause does not generate a `IEnumerable` which is what your view expects –  Feb 09 '18 at 03:54
  • Refer also [this answer](https://stackoverflow.com/questions/36759641/nested-query-mvc-linq/36759876#36759876) for a typical example of what you need to do (create view models to represent what you want to display in the view) –  Feb 09 '18 at 03:57

0 Answers0