DO 5.0.19

Last two queries throw NullReferenceException, because (obj as PocoImpl) = null and TextImpl can not be accessed

But first query do not throw exception, though has same (obj as PocoImpl).TextImpl expression

Can you detect unreachable branches in &&/|| conditions and replace with true/false constants?

Any workaround, besides custom IQueryPreprocessor with unreachable branches detection?

Example

using System;
using System.Diagnostics;
using System.Linq;
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(TestEntity));

        dc.UpgradeMode = DomainUpgradeMode.Recreate;

        using (var d = Domain.Build(dc))
        {
            using (var s = d.OpenSession())
            using (s.Activate())
            using (s.OpenTransaction())
            {
                var sw = Stopwatch.StartNew();

                var obj = new Poco();

                //OK
                Query.All<TestEntity>()
                  .Where(it => obj is PocoImpl && (obj as PocoImpl).TextImpl == "test")
                  .Count();

                //OK
                Query.All<TestEntity>()
                  .Where(it => obj.Text == it.Text && obj is PocoImpl)
                  .Count();

                //FAIL
                Query.All<TestEntity>()
                  .Where(it => obj.Text == it.Text && obj is PocoImpl && (obj as PocoImpl).TextImpl == "test")
                  .Count();

                //FAIL
                Query.All<TestEntity>()
                  .Where(it => obj is PocoImpl && obj.Text == it.Text && (obj as PocoImpl).TextImpl == it.Text)
                  .Count();

                Console.WriteLine(sw.Elapsed.TotalMilliseconds);
            }
        }
    }

    [HierarchyRoot]
    public class TestEntity : Entity
    {
        public TestEntity(Session session) : base(session)
        {
        }

        [Key] [Field(Nullable = false)] public int Id { get; set; }

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

    public class Poco
    {
        public string Text { get; set; }
    }

    public class PocoImpl : Poco
    {
        public string TextImpl { get; set; }
    }
}

asked Oct 01 '19 at 01:51

Gushchin%20Anton's gravatar image

Gushchin Anton
11272729

Be the first one to answer this question!
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