LINQ groupby to List <List <object>

I have an ASP.NET MVC view where I use LINQ to split a model into multiple object lists.

Below is the code where I get the correct list of objects in var data

, but I cannot then pass it to the object to access properties. I am getting the error

Unable to launch object of type 'Grouping [<> f__AnonymousType3'2 [System.DateTime, System.String], MVCProj.Models.tblData2017] for input' MVCProj.Models.tblData2017

Index.cshtml View:

@model IEnumerable<MVCProj.Models.tblData2017>

@{
    ViewBag.Title = "Index";
}

<h2>Title</h2>
@{
    var data = Model.Where(x => x.Start.Date == DateTime.UtcNow.Date)
                    .GroupBy(x => new { x.Start, x.Field2 }).ToList();
    for (int i = 0; i < data.Count; i++)
    {
        var rowItem = (List<tblCards2017>)data[i]; //<-Error on this line
        string header1 = $"{rowItem.Start.ToString("HH:mm")} {rowItem.Field2}";
    }
}

      

How can I accomplish any of the following

i) Press successfully var rowItem = (List<tblCards2017>)data[i];

or

ii) Use linq to get the correct object type List<List<tblCards2017> data = Model.Where.....

instead of the current typevar data = System.Collections.Generic.List<System.Linq.IGrouping<<>f__AnonymousType2<System.DateTime, string>, MVCProj.Models.tblData2017>>

+3


source share


1 answer


I think you are looking at this:

var data = Model.Where(x => x.Start.Date == DateTime.UtcNow.Date)
                .GroupBy(x => new { x.Start, x.Field2 })
                .Select(g=>g.ToList()).ToList();

      



Add call Select

to get list for each group

+6


source







All Articles