1

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;
}
lissajous
  • 371
  • 5
  • 17
  • What does "not group as expected" mean? We're not you and can't see your results or possibly know what you have in mind. Include them please. – Kamil Gosciminski Aug 07 '18 at 09:43
  • Consider upgrading to EF Core 2.1 which includes [LINQ GroupBy translation](https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-2.1#linq-groupby-translation) improvements. – Ivan Stoev Aug 07 '18 at 09:46
  • I know about EF Core 2.1 and improvements, but now it is impossible to upgrade. It not working properly inmemory now. – lissajous Aug 07 '18 at 09:48
  • Yeah, I understand. But improvements in EF Core usually means fixing bugs / wrong behaviors. It's possible that your query is hitting one of these, since I don't see anything wrong in your query building (no `Func<..>`s for instance). If you seek for workarounds, try removing the `OrderBy`, or try doing the whole grouping explicitly in memory (switch to LINQ to Objects before `GroupBy` - would require you to impement `GetHashCode` / `Equals` for `TheEntityGroupByData` – Ivan Stoev Aug 07 '18 at 09:59
  • Solved. It appeared that TheEntityGroupByData should be struct instad of class. More info here https://stackoverflow.com/questions/847066/group-by-multiple-columns – lissajous Aug 07 '18 at 10:29

0 Answers0