DO 5.0.18

Example:

namespace Sample
{
    using System.Linq;
    using System.Linq.Expressions;
    using NUnit.Framework;
    using Xtensive.Core;
    using Xtensive.IoC;
    using Xtensive.Orm;
    using Xtensive.Orm.Configuration;

    internal class Program
    {
        private static void Main(string[] args)
        {
            var dc = new DomainConfiguration("sqlserver",
                "Data Source=.; Initial Catalog=DO40-Tests; Integrated Security=True;");

            dc.Types.Register(typeof(ISimpleEntity));
            dc.Types.Register(typeof(ISystemEntity));
            dc.Types.Register(typeof(TestEntity));
            dc.Types.Register(typeof(TestEntity2));

            dc.UpgradeMode = DomainUpgradeMode.Recreate;

            using (var d = Domain.Build(dc))
            {
                using (var s = d.OpenSession())
                using (s.Activate())
                using (var t = s.OpenTransaction())
                {
                    new TestEntity(s) { Name = "test", SysName = "test" };
                    new TestEntity2(s) { Name = "test", SysName = "test" };

                    t.Complete();
                }

                using (var s = d.OpenSession())
                using (s.Activate())
                using (s.OpenTransaction())
                {
                    Assert.False(Query.All<TestEntity>().Any(e => e.SysName.IsNullOrEmpty()));
                    Assert.False(Query.All<TestEntity2>().Any(e => e.SysName.IsNullOrEmpty()));

                    CheckUnion();
                    CheckConcat();
                }
            }
        }

        private static void CheckUnion()
        {
            var anyEmpty0 = Query.All<TestEntity>().Select(e => new { e.Id, e.SysName })
                .Union(Query.All<TestEntity2>().Select(e => new { e.Id, e.SysName }))
                .Any(e => e.SysName.IsNullOrEmpty());

            Assert.False(anyEmpty0); // OK

            var anyEmpty1 = (Query.All<TestEntity>() as IQueryable<ISystemEntity>)
                .Union(Query.All<TestEntity2>())
                .Any(e => e.SysName.IsNullOrEmpty());

            Assert.False(anyEmpty1); // FAIL

            var anyEmpty2 = (Query.All<TestEntity>() as IQueryable<ISystemEntity>)
                .Union(Query.All<TestEntity2>())
                .ToArray()
                .Any(e => e.SysName.IsNullOrEmpty());

            Assert.False(anyEmpty2); // FAIL

            var anyEmpty3 = (Query.All<TestEntity>() as IQueryable<ISystemEntity>)
                .Union(Query.All<TestEntity2>())
                .Select(e => new { e.Id, e.SysName })
                .ToArray()
                .Any(e => e.SysName.IsNullOrEmpty());

            Assert.False(anyEmpty3); // FAIL
        }

        private static void CheckConcat()
        {
            var anyEmpty0 = Query.All<TestEntity>().Select(e => new { e.Id, e.SysName })
                .Concat(Query.All<TestEntity2>().Select(e => new { e.Id, e.SysName }))
                .Any(e => e.SysName.IsNullOrEmpty());

            Assert.False(anyEmpty0); // OK

            var anyEmpty1 = (Query.All<TestEntity>() as IQueryable<ISystemEntity>)
                .Concat(Query.All<TestEntity2>())
                .Any(e => e.SysName.IsNullOrEmpty());

            Assert.False(anyEmpty1); // FAIL

            var anyEmpty2 = (Query.All<TestEntity>() as IQueryable<ISystemEntity>)
                .Concat(Query.All<TestEntity2>())
                .ToArray()
                .Any(e => e.SysName.IsNullOrEmpty());

            Assert.False(anyEmpty2); // FAIL

            var anyEmpty3 = (Query.All<TestEntity>() as IQueryable<ISystemEntity>)
                .Concat(Query.All<TestEntity2>())
                .Select(e => new { e.Id, e.SysName })
                .ToArray()
                .Any(e => e.SysName.IsNullOrEmpty());

            Assert.False(anyEmpty3); // FAIL
        }

        /// <summary>
        /// Represent a basic entity
        /// </summary>
        public interface ISimpleEntity : IEntity
        {
            [Key]
            [Field(Nullable = false)]
            int Id { get; set; }

            /// <summary>
            /// Name
            /// </summary>
            [Field(Nullable = false, Length = 400)]
            string Name { get; set; }

            /// <summary>
            /// Description
            /// </summary>
            [Field(Length = 400)]
            string Description { get; set; }
        }

        /// <summary>
        /// Represents a system entity
        /// </summary>
        public interface ISystemEntity : ISimpleEntity
        {
            /// <summary>
            /// System name
            /// </summary>
            [Field(Nullable = false, Length = 400)]
            string SysName { get; set; }
        }

        [HierarchyRoot]
        public class TestEntity : Entity, ISystemEntity
        {
            /// <summary>Initializes a new instance of this class.</summary>
            /// <param name="session">The session.</param>
            public TestEntity(Session session) : base(session)
            {
            }

            public int Id { get; set; }

            /// <summary>
            /// Name
            /// </summary>
            public string Name { get; set; }

            /// <summary>
            /// Description
            /// </summary>
            public string Description { get; set; }

            /// <summary>
            /// System name
            /// </summary>
            public string SysName { get; set; }
        }

        [HierarchyRoot]
        public class TestEntity2 : Entity, ISystemEntity
        {
            /// <summary>Initializes a new instance of this class.</summary>
            /// <param name="session">The session.</param>
            public TestEntity2(Session session) : base(session)
            {
            }

            public int Id { get; set; }

            /// <summary>
            /// System name
            /// </summary>
            public string SysName { get; set; }

            /// <summary>
            /// Name
            /// </summary>
            public string Name { get; set; }

            /// <summary>
            /// Description
            /// </summary>
            public string Description { get; set; }
        }
    }
}

Generated SQL queries:

SELECT [a].[Id], [a].[TypeId], [a].[Name], [a].[Description], [a].[SysName] 
FROM (
    (SELECT [b].[Id], 100 AS [TypeId], [b].[Name], [b].[Description], [b].[SysName] FROM [dbo].[Program.TestEntity] [b]) 
    UNION 
    (SELECT [c].[Id], 101 AS [TypeId], [c].[SysName], [c].[Name], [c].[Description] FROM [dbo].[Program.TestEntity2] [c])) [a];

SELECT [a].[Id], [a].[TypeId], [a].[Name], [a].[Description], [a].[SysName] 
FROM (
    (SELECT [b].[Id], 100 AS [TypeId], [b].[Name], [b].[Description], [b].[SysName] FROM [dbo].[Program.TestEntity] [b]) 
    UNION  ALL 
    (SELECT [c].[Id], 101 AS [TypeId], [c].[SysName], [c].[Name], [c].[Description] FROM [dbo].[Program.TestEntity2] [c])) [a];

asked Apr 02 at 09:24

Gushchin%20Anton's gravatar image

Gushchin Anton
981520

edited Apr 02 at 09:26


One Answer:

Hello Anton,

Problem is confirmed, I have created task for the issue and will update answer when investigation shows something.

answered Apr 24 at 04:00

Alexey%20Kulakov's gravatar image

Alexey Kulakov
70215

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