3
1

Hi,

I'm having an issue when trying to group by and have a COUNT on object that might be NULL. The join on those is not performed correctly and aggregated values of the group by is wrong, could you look into it ?

Many Thanks,

Model

  [HierarchyRoot]
  public class Battery : Entity
  {
    [Field, Key]
    public long Id { get; private set; }
    [Field]
    public double Volts { get; set; }
    [Field]
    public int Amps { get; set; }
    [Field]
    public int Cell { get; set; }
  }

  [HierarchyRoot]
  public class Product : Entity
  {
    [Field, Key]
    public long Id { get; private set; }
    [Field]
    public string Description { get; set; }
    [Field]
    public Battery Battery { get; set; }
  }

Data population

List<Battery> batt = new List<Battery>()
      {
        null,
        new Battery(){Amps = 8, Cell = 12, Volts = 12.4},
        new Battery(){Amps = 2, Cell = 5, Volts = 3.7}
      };

      for (int i = 0; i < 10; i++)
      {
        var p = new Product()
        {
          Description = i % 3 != 0 ? "Lap:" + i%3 : null,
          Battery = batt[i%3]
        };
      }

Query

var x = Query.All<Product>().GroupBy(g=>g.Battery)
  .Select(g=>new{g.Key, Cnt = g.Count()});

Result

foreach(var e in x){
    output.WriteLine(e.Key + " - " + e.Cnt);
}
Count: Null Battery: 4
 - 0
Battery, (187246744) - 3
Battery, (187246745) - 3

The expected result is to have a count of 4 on Key null.

Sql

SELECT [a].[Id] AS [#a.Id], 1228 AS [#a.TypeId], [a].[Volts] AS [#a.Volts], [a].[Amps] AS [#a.Amps], [a].[Cell] AS [#a.Cell], (SELECT COUNT_BIG(*) FROM [dbo].[SPB.PartnerSoftware.Sfr.Reports.Product] [b] WHERE (**[b].[Battery.Id] = [c].[Battery.Id]**)) AS [c01umn] FROM (SELECT [d].[Battery.Id] AS [Battery.Id] FROM [dbo].[SPB.PartnerSoftware.Sfr.Reports.Product] [d] GROUP BY [d].[Battery.Id]) [c] LEFT OUTER JOIN [dbo].[SPB.PartnerSoftware.Sfr.Reports.Battery] [a] ON ([c].[Battery.Id] = [a].[Id])

Here, when Battery is null, the condition is not met, needed to add a or ([b].[Battery.Id] IS NULL and [c].[Battery.Id] IS NULL) ?

In Memory LINQ

alt text

asked Apr 26 '18 at 02:29

rle's gravatar image

rle
99559

Hi, do you have any update on this ? This is really important for us.

Thanks

(Apr 27 '18 at 05:16) rle rle's gravatar image

Any updates on this ?

Thanks

(May 14 '18 at 03:07) rle rle's gravatar image

Hello. This problem is investigating now.

(May 17 '18 at 08:10) Alexey Kulakov Alexey%20Kulakov's gravatar image

2 Answers:

Hello rle

Try the 5.0.18 Beta 2. We have made changes for grouping there

answered Jun 13 '18 at 00:19

Alexey%20Kulakov's gravatar image

Alexey Kulakov
77225

Thank you for this update

(Jun 25 '18 at 05:19) rle rle's gravatar image
1

Works now?

(Jun 25 '18 at 05:34) Alexey Kulakov Alexey%20Kulakov's gravatar image

Yes thanks, tested with 5.0.18b2

(Jun 25 '18 at 07:05) rle rle's gravatar image

This bug is not fixed if the key of grouping is not a field of entity (e.g. field of anonymous type)

var x = Query.All<Product>()
  .Select(p => new { p.Battery })
  .GroupBy(g=>g.Battery)
  .Select(g=>new{g.Key, Cnt = g.Count()});

answered Apr 05 '19 at 06:10

Platonov's gravatar image

Platonov
5778

edited Apr 05 '19 at 06:33

Yes. Fix didn't cover such cases. Added adjustments in developing branch

(Apr 08 '19 at 09:25) Alexey Kulakov Alexey%20Kulakov's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA