Model:

[Serializable]
[KeyGenerator(KeyGeneratorKind.None)]
public abstract class EntityBase : Entity
{
    protected EntityBase(Guid id) : base(id)
    {
    }

    [Field]
    [Key]
    public Guid Id { get; }
}

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

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

    [Field]
    public TestEntity1 Link { get; set; }
}

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

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

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

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

Code:

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

        dc.Types.Register(typeof(Program).Assembly);

        using (var d = Domain.Build(dc))
        {
            ExecuteInNewSession(d, () =>
            {
                for (var i = 0; i < 2000; i++)
                {
                    new TestEntity1 { Name = "124" };
                }
            });

            ExecuteInNewSession(d,
                () =>
                {
                    var arr = Session.Current.Query.All<TestEntity1>().Select(e1 => e1.Id).ToArray();

                    Session.Current.Query.All<TestEntity>()
                        .Where(e => arr.Contains(e.Link.Id))
                        .Count();

                    Session.Current.Query.All<TestEntity>()
                        .Where(e => Ids().Contains(e.Link.Id))
                        .Count();

                    Session.Current.Query.All<TestEntity>()
                        .Where(e => Session.Current.Query.All<TestEntity1>().Any(e1 => e1.Id == e.Link.Id))
                        .Count();

                    var ids = Enumerable.Range(0, 2000)
                    .Select(e => Guid.NewGuid())
                    .ToArray();

                    var query = Session.Current.Query.All<TestEntity>()
                    .Where(e => e.Link.Id.In(ids));

                    Session.Current.Query.All<TestEntity>()
                    .Count(t => query.Any(it => it.Link.Id == t.Id));

                    // Throw exception
                    Session.Current.Query.All<TestEntity>()
                    .Count(t => Query().Any(it => it.Link.Id == t.Id));

                    // Throw exception
                    Session.Current.Query.All<TestEntity>()
                        .Where(e => Session.Current.Query.All<TestEntity1>().Select(e1 => e1.Id).ToArray().Contains(e.Link.Id))
                        .Count();
                });
        }
    }

    private static Guid[] Ids()
    {
        return Session.Current.Query.All<TestEntity1>().Select(e1 => e1.Id).ToArray();
    }

    private static void ExecuteInNewSession(Domain d, Action action)
    {
        using (var s = d.OpenSession())
        using (s.Activate())
        using (var t = s.OpenTransaction())
        {
            action.Invoke();
            t.Complete();
        }
    }

    private static IQueryable<TestEntity> Query()
    {
        var ids = Enumerable.Range(0, 2000).Select(e => Guid.NewGuid()).ToArray();

        return Session.Current.Query.All<TestEntity>().Where(e => e.Link.Id.In(ids));
    }
}

Last query throw exception:

An unhandled exception of type 'Xtensive.Orm.QueryTranslationException' occurred in Xtensive.Orm.dll

Additional information: Unable to translate 'Query.All().Where(e => .Current.Query.All().Select(e1 => e1.Id).ToArray().Contains(e.Link.Id)).Count()' expression. See inner exception for details.

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 System.Linq.Queryable.Count[TSource](IQueryable`1 source)
at Sample.Program.<>c.<Main>b__0_1() in c:\users\gushin\documents\visual studio 2015\Projects\CleanDO2\CleanDO2\Program.cs:line 56
at Sample.Program.ExecuteInNewSession(Domain d, Action action) in c:\users\gushin\documents\visual studio 2015\Projects\CleanDO2\CleanDO2\Program.cs:line 77
at Sample.Program.Main(String[] args) in c:\users\gushin\documents\visual studio 2015\Projects\CleanDO2\CleanDO2\Program.cs:line 30
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

Inner exception:

'Contains' method is only supported for root expressions or subqueries.

at Xtensive.Orm.Linq.Translator.VisitContains(Expression source, Expression match, Boolean isRoot)
at Xtensive.Orm.Linq.Translator.VisitQueryableMethod(MethodCallExpression mc, QueryableMethodKind methodKind)
at Xtensive.Linq.QueryableVisitor.VisitMethodCall(MethodCallExpression mc)
at Xtensive.Orm.Linq.Translator.VisitMethodCall(MethodCallExpression mc)
at Xtensive.Linq.ExpressionVisitor`1.Visit(Expression e)
at Xtensive.Orm.Linq.Translator.Visit(Expression e)
at Xtensive.Orm.Linq.Translator.VisitLambda(LambdaExpression le)
at Xtensive.Orm.Linq.Translator.VisitWhere(Expression expression, LambdaExpression le)
at Xtensive.Orm.Linq.Translator.VisitQueryableMethod(MethodCallExpression mc, QueryableMethodKind methodKind)
at Xtensive.Linq.QueryableVisitor.VisitMethodCall(MethodCallExpression mc)
at Xtensive.Orm.Linq.Translator.VisitMethodCall(MethodCallExpression mc)
at Xtensive.Linq.ExpressionVisitor`1.Visit(Expression e)
at Xtensive.Orm.Linq.Translator.Visit(Expression e)
at Xtensive.Orm.Linq.Translator.VisitSequence(Expression sequenceExpression, Expression expressionPart)
at Xtensive.Orm.Linq.Translator.VisitWhere(Expression expression, LambdaExpression le)
at Xtensive.Orm.Linq.Translator.VisitQueryableMethod(MethodCallExpression mc, QueryableMethodKind methodKind)
at Xtensive.Linq.QueryableVisitor.VisitMethodCall(MethodCallExpression mc)
at Xtensive.Orm.Linq.Translator.VisitMethodCall(MethodCallExpression mc)
at Xtensive.Linq.ExpressionVisitor`1.Visit(Expression e)
at Xtensive.Orm.Linq.Translator.Visit(Expression e)
at Xtensive.Orm.Linq.Translator.VisitSequence(Expression sequenceExpression, Expression expressionPart)
at Xtensive.Orm.Linq.Translator.VisitSort(Expression expression)
at Xtensive.Orm.Linq.Translator.VisitQueryableMethod(MethodCallExpression mc, QueryableMethodKind methodKind)
at Xtensive.Linq.QueryableVisitor.VisitMethodCall(MethodCallExpression mc)
at Xtensive.Orm.Linq.Translator.VisitMethodCall(MethodCallExpression mc)
at Xtensive.Linq.ExpressionVisitor`1.Visit(Expression e)
at Xtensive.Orm.Linq.Translator.Visit(Expression e)
at Xtensive.Orm.Linq.Translator.VisitSequence(Expression sequenceExpression, Expression expressionPart)
at Xtensive.Orm.Linq.Translator.VisitTake(Expression source, Expression take)
at Xtensive.Orm.Linq.Translator.VisitQueryableMethod(MethodCallExpression mc, QueryableMethodKind methodKind)
at Xtensive.Linq.QueryableVisitor.VisitMethodCall(MethodCallExpression mc)
at Xtensive.Orm.Linq.Translator.VisitMethodCall(MethodCallExpression mc)
at Xtensive.Linq.ExpressionVisitor`1.Visit(Expression e)
at Xtensive.Orm.Linq.Translator.Visit(Expression e)
at Xtensive.Orm.Linq.Translator.VisitSequence(Expression sequenceExpression, Expression expressionPart)
at Xtensive.Orm.Linq.Translator.VisitAggregateSource(Expression source, LambdaExpression aggregateParameter, AggregateType aggregateType, Expression visitedExpression)
at Xtensive.Orm.Linq.Translator.VisitAggregate(Expression source, MethodInfo method, LambdaExpression argument, Boolean isRoot, MethodCallExpression expressionPart)
at Xtensive.Orm.Linq.Translator.VisitQueryableMethod(MethodCallExpression mc, QueryableMethodKind methodKind)
at Xtensive.Linq.QueryableVisitor.VisitMethodCall(MethodCallExpression mc)
at Xtensive.Orm.Linq.Translator.VisitMethodCall(MethodCallExpression mc)
at Xtensive.Linq.ExpressionVisitor`1.Visit(Expression e)
at Xtensive.Orm.Linq.Translator.Visit(Expression e)
at Xtensive.Orm.Linq.Translator.Translate[TResult]()
at Xtensive.Orm.Linq.QueryProvider.Translate[TResult](Expression expression, CompilerConfiguration compilerConfiguration)

asked Oct 13 '16 at 09:57

Anton%20Guschin's gravatar image

Anton Guschin
73273035

edited Oct 14 '16 at 03:18


One Answer:

Hello Anton

I've checked the issue. The case is confirmed so I created task for this. We'll start investigation later.

answered Dec 05 '16 at 06:22

Alexey%20Kulakov's gravatar image

Alexey Kulakov
51215

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