I have query like this for TheEntity type:
var source = await _repository.Queryable.AsNoTracking()
.Where(Condition1())
.Where(Condition2(params))
.GroupBy(GroupByFunction)
.Select(SelectFunction)
.OrderBy(o => o.Field1)
.ToAsyncEnumerable().ToList();
This query select all records which fulfill conditions: Condition1, Condition2, but does not group them as I expected.
SelectFunction and GroupByFunction looks like below:
private readonly Expression<Func<IGrouping<TheEntityGroupByData, TheEntity>, TheEntitySelectData>> SelectFunction =
e => new TheEntitySelectData()
{
Field1 = e.Key.Field1,
Field2 = e.Key.Field2,
...
FieldN = e.Key.FieldN,
Field(N+1) = e.Sum(x=>x.Field(N+1)),
...
FieldK = e.Sum(x=>x.FieldK),
};
private readonly Expression<Func<TheEntity, TheEntityGroupByData>> GroupByFunction =
e => new TheEntityByData()
{
Field1 = e.Field1,
Field2 = e.Field2,
...
FieldN = e.Key.FieldN,
};
TheEntityGroupByData, TheEntitySelectData are helper DTO/PO_Os types. I intendent to fire grouping on database rather than server, but this behavior does not work even in server memory.
I use .Net Core 2.0.5 and EntityFrameworkCore 2.0.2.
My question is where is the problem in this approach?
edit - What I mean query not work as I expected is that: If I will have two the same records in db (same by grouping key) that fulfill Condition1 and Condition2, query will return 2 instead of one records.
Filtering conditions looks like below:
private static Expression<Func<TheEntity, bool>> Condition1()
{
return (e) => e.Field1 == SOME_CONSTANT;
}
private static Expression<Func<TheEntity, bool>> Condition2(RequestParam param)
{
Expression<Func<TheEntity, bool>> whereSth;
if (param.some.HasValue)
whereSth = (e) => e.Field2 <= param.some.Value;
else
whereSth = (e) => true;
return whereSth;
}