I try to group by StringId and then by Name
var a = myList
.GroupBy(item => new TcGroupByKey()
{StringId = item.Id, ChannelName = item.ChannelName}).ToList();
I have created this inner class TcGroupByKey
so I can pass the result to TrackingChannelRow ctor
and will get strongly type argument and not object
public TrackingChannelRow(ManageTcModel.TcGroupByKey tcGroupByKey,
IGrouping<ManageTcModel.TcGroupByKey, TrackingChannelItem> subChannels,
IEnumerable<Manager.TrackingChannels.Model.ToolbarItem> toolbars,
IEnumerable<Manager.TrackingChannels.Model.BundleItem> bundles)
{
But the group by doesn’t work.
What am I doing wrong?
I think that what you’re looking for is to group by composite key…
Something similar to what Marc described in this post Linq to SQL with Group by
The key is to use ‘anonymous’ types – not the named one like you did – that LINQ can translate into SQL, otherwise there’s an issue and that code can only run ‘post SQL’ (so you enumerate first then group but lose on the SQL integration, performance).
So, in your case it’d be something like this…
…instead of
new TcGroupByKey() {StringId = item.Id, ChannelName = item.ChannelName}There is similar problem with doing a ‘Select’ into anonymous (works) or named type (doesn’t – unless, as I mentioned above, you separate the SQL and C#-only expressions part, a bit simplified).
hope this helps