I have a problem where the we have a lot of data grouped into what are called stages. The front end wants to display this data, grouped in stages, before rendering it. The number of stages is variable. So originally they were making an api per stage to bunching up the data before binding to a grid control.
When the number of stages become quite high, it becomes too slow. The browser will queue api requests and the overall response time would be very long. Better - we decided - would be to have a single api call that can deal with grouping AND paging. That is, you could say 'take=30' and it would give you 30 records of EACH stage, for however many stages there are.
So the query on the back end looks like what I have below. The problems is that the sorting of the data, before the skip and take is on a dynamic field, and this is where it falls over. Dynamic Linq doesn't seem to work (although it works perfectly fine for normal queries outside the GroupBy call), and I haven't been able to get any of the huge number of extension methods out there working either. They are all some variation of the errors you would find below: (Keep in mind the problem is getting it working with linq to sql)
TestEntities context = new TestEntities();
List<TestTable1> result;
// This works (Hard coding at design time)
result = context.TestTable1.GroupBy(x => x.StageId)
.SelectMany(x => x.OrderBy(y => y.StageId).Skip(1).Take(1)).ToList();
// This works (using a hard coded function at design time)
Func<TestTable1, int> orderByExpression = x => x.StageId;
result = context.TestTable1.GroupBy(x => x.StageId)
.SelectMany(x => x.OrderBy(orderByExpression).Skip(1).Take(1)).ToList();
// This doesn't work. Dynamic linq
result = context.TestTable1.GroupBy(x => x.StageId)
.SelectMany(x => x.AsQueryable().OrderBy("Name").Skip(1).Take(1)).ToList();
// This doesn't work. Can't convert an object to a primitive type
Func<TestTable1, object> orderByObjectExpression = x => x.StageId;
result = context.TestTable1.GroupBy(x => x.StageId)
.SelectMany(x => x.AsQueryable().OrderBy(orderByObjectExpression).Skip(1).Take(1)).ToList();
// This doesn't work. Same as above
Func<TestTable1, dynamic> orderByDynamicExpression = x => x.StageId;
result = context.TestTable1.GroupBy(x => x.StageId)
.SelectMany(x => x.AsQueryable().OrderBy(orderByObjectExpression).Skip(1).Take(1)).ToList();
Console.WriteLine(JsonConvert.SerializeObject(result));
Console.ReadKey();