DO 5.0.18

SelectMany with two nested subqueries

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.Types.Register(typeof(TestEntity2));
        dc.Types.Register(typeof(TestEntity3));

        dc.UpgradeMode = DomainUpgradeMode.Recreate;

        using (var d = Domain.Build(dc))
        {
            using (var s = d.OpenSession())
            using (s.Activate())
            using (s.OpenTransaction())
            {
                Query.All<TestEntity>()
                    .SelectMany(entity => Query.All<TestEntity2>()
                        .Where(entity2 => Query.All<TestEntity3>()
                            .Any(entity3 => entity3.Link == entity
                                            && entity3.Link2 == entity2)))
                    .ToArray();

                //FAIL
                Query.All<TestEntity>()
                    .SelectMany(entity => Query.All<TestEntity2>()
                        .Where(entity2 => Query.All<TestEntity3>()
                            .Any(entity3 => entity3.Link == entity)))
                    .ToArray();
            }
        }
    }

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

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

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

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

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

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

        /// <summary>
        /// Link
        /// </summary>
        [Field]
        public TestEntity Link { get; set; }

        /// <summary>
        /// Link2
        /// </summary>
        [Field]
        public TestEntity2 Link2 { get; set; }
    }
}

Exception

Unhandled Exception: Xtensive.Orm.QueryTranslationException: Unable to translate 'Query.All().SelectMany(entity => Query.All().Where(entity2 => Query.All().Any(entity3 => (entity3.Link == entity))))' expression. See inner exception for details. ---> Syste
m.NullReferenceException: Object reference not set to an instance of an object.
   at Xtensive.Orm.Rse.Transformation.ApplyFilterRewriter.VisitMethodCall(MethodCallExpression mc)
   at Xtensive.Linq.ExpressionVisitor`1.Visit(Expression e)
   at Xtensive.Linq.ExpressionVisitor.VisitBinary(BinaryExpression b)
   at Xtensive.Linq.ExpressionVisitor`1.Visit(Expression e)
   at Xtensive.Orm.Rse.Transformation.ApplyFilterRewriter.Rewrite(Expression`1 predicate, ColumnCollection predicateColumns, ColumnCollection currentColumns)
   at Xtensive.Orm.Rse.Transformation.ApplyProviderCorrectorRewriter.ConvertGenericApply(ApplyProvider provider, CompilableProvider left, CompilableProvider right)
   at Xtensive.Orm.Rse.Transformation.ApplyProviderCorrectorRewriter.VisitApply(ApplyProvider provider)
   at Xtensive.Orm.Rse.Providers.ProviderVisitor.Visit(CompilableProvider cp)
   at Xtensive.Orm.Rse.Transformation.ApplyProviderCorrectorRewriter.VisitSelect(SelectProvider provider)
   at Xtensive.Orm.Rse.Providers.ProviderVisitor.Visit(CompilableProvider cp)
   at Xtensive.Orm.Rse.Transformation.ApplyProviderCorrectorRewriter.Rewrite(CompilableProvider rootProvider)
   at Xtensive.Orm.Rse.Transformation.ApplyProviderCorrector.Process(CompilableProvider rootProvider)
   at Xtensive.Orm.Rse.Compilation.CompositePreCompiler.Process(CompilableProvider rootProvider)
   at Xtensive.Orm.Providers.CompilationService.Compile(CompilableProvider provider, CompilerConfiguration configuration)
   at Xtensive.Orm.Linq.Translator.Translate[TResult](ProjectionExpression projection, IEnumerable`1 tupleParameterBindings)
   at Xtensive.Orm.Linq.Translator.Translate[TResult]()
   at Xtensive.Orm.Linq.QueryProvider.Translate[TResult](Expression expression, CompilerConfiguration compilerConfiguration)
   --- End of inner exception stack trace ---
   at Xtensive.Orm.Linq.QueryProvider.Translate[TResult](Expression expression, CompilerConfiguration compilerConfiguration)
   at Xtensive.Orm.Linq.QueryProvider.Translate[TResult](Expression expression)
   at Xtensive.Orm.Linq.QueryProvider.Execute[TResult](Expression expression)
   at Xtensive.Orm.Linq.Queryable`1.GetEnumerator()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at Sample.Program.Main(String[] args) in C:\Projects\CleanDO\Sample\Program.cs:line 34

asked Oct 29 at 05:19

Gushchin%20Anton's gravatar image

Gushchin Anton
9121921

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