Skip to content

Can't translate GroupBy with Conditional Aggregate #18836

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
hamedmaleki1366 opened this issue Nov 11, 2019 · 1 comment · Fixed by #21907
Closed

Can't translate GroupBy with Conditional Aggregate #18836

hamedmaleki1366 opened this issue Nov 11, 2019 · 1 comment · Fixed by #21907

Comments

@hamedmaleki1366
Copy link

hamedmaleki1366 commented Nov 11, 2019

this is my Class and code to

public enum StatusType
{
  Waiting = 0,
  Reading = 1,
  Printing = 2
}
public class DistributeListDetail
{
 public int Id {get;set;}
 public string AddressCode {get;set;}
 public StatusType Status {get;set;}
}

now i have a Query like this

var  result = Context.DistributeListDetails.Join().Where()
.GroupBy(x=> new { Path = x.AddressCode.SubString(0,4) })
.Select (x=> new {
 AddressCodeGrouped = x.Key.Path,                    
                    DistributedCustomerCount = x.Count(),
                    WaitingCount = x.Count(c => c.Status == StatusType.Waiting),
                    ReadCount = x.Count(c => c.Status == StatusType.Reading ),
}).ToListAsync();

the DistributedCustomerCount , WaitingCount , ReadCount is always Equals . maybe the
Conditionals at Count method not Translating.

EF Core version: 3.0
Database provider: (Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (.NET Core 3.0)
Operating system: Windows 10
IDE: (Visual Studio 2019 16.3)

@liamwang
Copy link

I have the same problem.

var result = Entities
    .GroupBy(x => x.UserId)
    .Select(g => new 
    {
        UserId = g.Key,
        TotalScore1 = g.Sum(x => x.Type == 1 ? x.Score : 0),
        TotalScore2 = g.Sum(x => x.Type == 2 ? x.Score : 0)
    });

This throws error: 0 ' could not be translated.

Current workaround:

var result = Entities
    .Select(x => new 
    {
        x.UserId,
        Score1 = x.Type == 1 ? x.Score : 0,
        Score2 = x.Type == 2 ? x.Score : 0
    })
    .GroupBy(x => x.UserId)
    .Select(g => new 
    {
        UserId = g.Key,
        TotalScore1 = g.Sum(x => x.Score1),
        TotalScore2 = g.Sum(x => x.Score2)
    });

It's a little bit messy.

smitpatel added a commit that referenced this issue Aug 3, 2020
smitpatel added a commit that referenced this issue Aug 3, 2020
smitpatel added a commit that referenced this issue Aug 3, 2020
smitpatel added a commit that referenced this issue Aug 3, 2020
@smitpatel smitpatel modified the milestones: 5.0.0, 5.0.0-rc1 Aug 3, 2020
smitpatel added a commit that referenced this issue Aug 3, 2020
@ghost ghost closed this as completed in #21907 Aug 3, 2020
ghost pushed a commit that referenced this issue Aug 3, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0-rc1, 5.0.0 Nov 7, 2020
This issue was closed.
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants