I am having a hard time grouping a dbset (EntityFramework) by two fields and sending that output to a strongly typed view.
When I use an anonymous type for the composite key I get the right output. A list containing one item and that item in turn has two or more grouping items.
Now if I use a class instead I get a list of two items and in turn each item has one grouping item.
var output = context.Transfers.GroupBy(t=> new { t.TNumber, t.Type}).ToList();
var output2 = context.Transfers.AsEnumerable()
.GroupBy(t => new OTSpecs(t.TNumber, t.Type)).ToList();
OTSpecs
is just a simple class, with those public fields and a parameter constructor.
I need to add the AsEnumerable()
otherwise I get a System.NotSupportedException
Only parameterless constructors and initializers are supported in LINQ to Entities
Also because I need to define the model in the view like this
@model IEnumerable<IGrouping<OTSpecs, Transfer>>
unless of course it is possible to replace OTSpecs in that line with the anonymous type. But I don't know how.
My question is why those lines of code produce a different output? Is it possible to define the model in the view replacing the OTSpecs for a anonymous type?