Translation of DO is differs from result with ToArray()

Example:

namespace Test
{
    using System;
    using System.Linq;
    using Xtensive.Orm;
    using Xtensive.Orm.Configuration;

    [HierarchyRoot]
    public class Owner : Entity
    {
        [Field]
        [Key]
        public Guid Id { get; set; }

        [HierarchyRoot]
        public class MyEntity : Entity
        {
            [Field]
            [Key]
            public Guid Id { get; set; }

            [Field]
            public DateTime? DateTime { get; set; }

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

            [Field]
            public bool OnlyCurrentType { get; set; }

            [Field(Nullable = false)]
            public Owner Owner { get; set; }
        }
    }

    class Program
    {

        private static void Main(string[] args)
        {

            var conf = new DomainConfiguration("sqlserver://localhost/DO40-Tests");
            conf.UpgradeMode = DomainUpgradeMode.Recreate;
            conf.Types.Register(typeof(Owner.MyEntity));

            using (var d1 = Domain.Build(conf))
            {
                using (var s = d1.OpenSession())
                using (s.Activate())
                {
                    using (var t = s.OpenTransaction())
                    {
                        var o = new Owner() {  };
                        var o1 = new Owner() {  };
                        var aa = new Owner.MyEntity() {Owner = o, EE = "123", DateTime = DateTime.Now };
                        var aa2 = new Owner.MyEntity() { Owner = o, EE = "234", OnlyCurrentType = true, DateTime = DateTime.Now };
                        var aa3 = new Owner.MyEntity() {  Owner = o, EE = "234", OnlyCurrentType = true, DateTime = DateTime.Now };
                        var aa4 = new Owner.MyEntity() {  Owner = o, EE = "234" };
                        var aa5 = new Owner.MyEntity() { Owner = o1, OnlyCurrentType = true, EE = "234" };
                        t.Complete();
                    }
                }

                using (var s = d1.OpenSession())
                using (s.Activate())
                {
                    using (var t = s.OpenTransaction())
                    {
                        var q1 =
                            Query.All<Owner.MyEntity>()
                                .Select(q => new { OnlyCurrentType = !q.OnlyCurrentType, FinToolKind = q.OnlyCurrentType ? null : q.Owner })
                                .Distinct();

                        var qa2 = q1.ToArray();                      // 3 Items (WRONG)
                        var qa3 = q1.ToArray().Distinct().ToArray(); // 2 Items

                        var my = from q in Query.All<Owner.MyEntity>() //.ToArray() //- fixes
                                 group q by q.DateTime into gr
                                 select new { gr.Key, gr.First().EE };
                        var arr = my.ToArray();
                        t.Complete();
                    }
                }
            }
        }
    }
}

asked Dec 10 '15 at 04:10

pil0t's gravatar image

pil0t
207535763


One Answer:

Hi pil0t

We'll investigate both problems. For now I have some workaround for case with distinct. You may rewrite query like

Query.All<owner.myentity>()
     .Select(q => new {
        OnlyCurrentType = !q.OnlyCurrentType,
        FinToolKind = q.OnlyCurrentType 
           ? null
           : (Guid?)q.Owner.Id
     })
     .Distinct()
     .LeftJoin(Query.All<owner>(),e=>e.FinToolKind, owner => owner.Id, (e, o)=>new {
        e.OnlyCurrentType,
        FinToolKind = o
     });

answered Dec 14 '15 at 03:53

Alexey%20Kulakov's gravatar image

Alexey Kulakov
48215

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