Hi

DO version 5.0.1

Without ToArray SelectMany return wrong results

Sample:

using System;
using System.Linq;

using NUnit.Framework;

using Xtensive.Orm;
using Xtensive.Orm.Configuration;

using Domain = Xtensive.Orm.Domain;

class Program
{
    static void Main(string[] args)
    {
        var dc = new DomainConfiguration("sqlserver://localhost/DO40-Tests");

        dc.Types.Register(typeof(TestEntity));

        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 (var t = session.OpenTransaction())
            {
                new TestEntity { TestField = "Test" };
                new TestEntity { TestField = "Test" };

                new TestEntity { TestField = "Test1" };
                new TestEntity { TestField = "Test1" };
                new TestEntity { TestField = "Test1" };

                new TestEntity { TestField = "Test2" };

                new TestEntity { TestField = "Test3" };

                t.Complete();
            }

            using (var session = domain.OpenSession(sessionConfiguration))
            using (session.Activate())
            using (var t = session.OpenTransaction())
            {
                var withToArrayGroupCount =
                    Session.Current.Query.All<TestEntity>()
                        .GroupBy(e => e.TestField)
                        .Where(g => g.Count() > 1)
                        .ToArray()
                        .Count();

                Assert.AreEqual(2, withToArrayGroupCount);

                var withoutToArrayGroupCount =
                    Session.Current.Query.All<TestEntity>()
                        .GroupBy(e => e.TestField)
                        .Count(g => g.Count() > 1);

                Assert.AreEqual(2, withoutToArrayGroupCount);

                var withToArray =
                    Session.Current.Query.All<TestEntity>()
                        .GroupBy(e => e.TestField)
                        .Where(g => g.Count() > 1)
                        .ToArray()
                        .SelectMany(g => g)
                        .Count();

                Assert.AreEqual(5, withToArray);

                var withoutToArray =
                    Session.Current.Query.All<TestEntity>()
                        .GroupBy(e => e.TestField)
                        .Where(g => g.Count() > 1)
                        .SelectMany(g => g)
                        .Count();

                // withoutToArray = 7, but expected 5
                Assert.AreEqual(5, withoutToArray);
            }
        }
    }
}

[HierarchyRoot]
[Serializable]
public class TestEntity : Entity
{
    [Key]
    [Field(Nullable = false)]
    public Guid Id { get; private set; }

    [Field]
    public string TestField { get; set; }
}

asked Mar 06 '15 at 08:17

Anton%20Guschin's gravatar image

Anton Guschin
73293035


One Answer:

Hello Anton Guschin

Thank you for bug report.

answered Mar 10 '15 at 04:29

Alexey%20Kulakov's gravatar image

Alexey Kulakov
52715

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