Hi,
There is a bug with GroupJoin on nullable key that leads to invalid grouping. If I try to GroupJoin EntityA with EntityB by nullable field, the join key of EntityA without matching keys of EntityB starts matching all null keys from EntityB
Code reproducing the issue:
namespace Sample
{
using System;
using System.Linq;
using Xtensive.Orm;
using Xtensive.Orm.Configuration;
[HierarchyRoot]
public class SimpleEntity : Entity
{
[Field][Key] public int Id { get; set; }
[Field] public string Name { get; set; }
}
[HierarchyRoot]
public class EntityWithLink : Entity
{
[Field][Key] public int Id { get; set; }
[Field] public SimpleEntity Link { get; set; }
}
class Program
{
static void Main(string[] args)
{
var dc = new DomainConfiguration("sqlserver", "Data Source=.; Initial Catalog=DO40-Tests;Connection Timeout=300;Integrated Security = true;Max pool size=10");
dc.Types.Register(typeof(Program).Assembly);
dc.UpgradeMode = DomainUpgradeMode.Recreate;
var sessionConfiguration = new SessionConfiguration(SessionOptions.AutoActivation | SessionOptions.ServerProfile);
using (var domain = Domain.Build(dc))
{
using (var session = domain.OpenSession(sessionConfiguration))
using (session.Activate())
using (session.OpenTransaction(TransactionOpenMode.New))
{
var withLink = new SimpleEntity { Name = "WithLink" };
var withoutLink1 = new SimpleEntity { Name = "WithoutLink1" };
var withoutLink2 = new SimpleEntity { Name = "WithoutLink2" };
new EntityWithLink { Link = withLink, };
new EntityWithLink { Link = withLink, };
new EntityWithLink { Link = null, };
new EntityWithLink { Link = null, };
new EntityWithLink { Link = null, };
var counts = Query.All<SimpleEntity>() // .AsEnumerable()
.GroupJoin(Query.All<EntityWithLink>(),
simpleEntity => simpleEntity,
entityWithLink => entityWithLink.Link,
(simpleEntity, links) => new
{
simpleEntity.Name,
Count = links.Count()
})
.ToArray();
foreach (var count in counts)
{
Console.WriteLine($"{count.Name} - {count.Count}");
}
}
}
}
}
}
Output of the sample:
WithLink - 2
WithoutLink1 - 3
WithoutLink2 - 3
The problem has appeared in 5.0.18 version (5.0.17 is ok)
asked
Apr 05 '19 at 05:55
Platonov
5●7●7●8