namespace TestAggregateWithCustomCompiler
{
    using System;
    using System.Linq;
    using System.Linq.Expressions;

    using NUnit.Framework;

    using Xtensive.Core;
    using Xtensive.Linq;
    using Xtensive.Orm;
    using Xtensive.Orm.Configuration;

    public class TestCl
    {
        private Domain domain;

        [TestFixtureSetUp]
        public void SetUp()
        {
            var domainConfiguration = new DomainConfiguration("sqlserver://localhost/DO40-Tests")
                { UpgradeMode = DomainUpgradeMode.Recreate };

            domainConfiguration.Types.Register(typeof(TestCl).Assembly);
            this.domain = Domain.Build(domainConfiguration);
            using (var s = domain.OpenSession())
            using (s.Activate())
            using (var t = s.OpenTransaction())
            {
                new MyEntity() { Money = 5, Price = 4, Amount = 2 };
                new MyEntity() { Money = 3, Price = 2, Amount = 1 };

                t.Complete();
            }
        }

        [Test]
        public void Test()
        {
            using (var session = domain.OpenSession())
            using (var t = session.OpenTransaction())
            {
                var query = from q in session.Query.All<MyEntity>()
                            select new { Item = q, FakeKey = 0 }
                            into i group i by i.FakeKey;
                var aggregateQuery = from q in query
                                     select new 
                                     {
                                         SumVirtual = q.Sum(b => b.Item.VirtualDecimalValue), 
                                         SumReal = q.Sum(b => b.Item.Money)
                                     };
                var res = aggregateQuery.ToArray();
            }
        }
    }

    [CompilerContainer(typeof(Expression))]
    public static class CustomLinqCompilerContainer
    {
        public static Expression<Func<MyEntity, decimal>> MyEntTextExpression = a => a.Price * a.Amount;

        [Compiler(typeof(MyEntity), "VirtualDecimalValue", TargetKind.PropertyGet)]
        public static Expression MyEntText(Expression assignmentExpression)
        {
            return MyEntTextExpression.BindParameters(assignmentExpression);
        }
    }

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

        public decimal VirtualDecimalValue { get; set; }

        [Field]
        public decimal Money { get; set; }

        [Field]
        public decimal Price { get; set; }

        [Field]
        public int Amount { get; set; }
    }
}

Exception:

    Xtensive.Orm.QueryTranslationException : Unable to translate 'Query.All().Select(q => new @<Item, FakeKey>(
  q,
  0
)).GroupBy(i => i.FakeKey).Select(q => new @<SumVirtual, SumReal>(
  q.Sum(b => b.Item.VirtualDecimalValue),
  q.Sum(b => b.Item.Money)
))' expression. See inner exception for details.
  ----> System.ArgumentOutOfRangeException : Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at Xtensive.Orm.Linq.QueryProvider.Translate(Expression expression)
at Xtensive.Orm.Linq.QueryProvider.Execute(Expression expression)
at Xtensive.Orm.Linq.Queryable`1.GetEnumerator()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray(IEnumerable`1 source)
at TestAggregateWithCustomCompiler.TestCl.Test() in TestCl.cs: line 52
--ArgumentOutOfRangeException
at System.Collections.Generic.List`1.get_Item(Int32 index)
at Xtensive.Storage.Rse.Providers.Compilable.AggregateProvider..ctor(CompilableProvider source, Int32[] groupIndexes, AggregateColumnDescriptor[] columnDescriptors)
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.VisitNewExpressionArguments(NewExpression n)
at Xtensive.Orm.Linq.Translator.VisitNew(NewExpression newExpression)
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.BuildProjection(LambdaExpression le)
at Xtensive.Orm.Linq.Translator.VisitSelect(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.Translate()
at Xtensive.Orm.Linq.QueryProvider.Translate(Expression expression)

asked Jun 15 '12 at 06:58

A%20Volkov's gravatar image

A Volkov
17224


One Answer:

Hello A Volkov,

fix will be included in 4.5.3 RC4 which is expected to be released very soon.

answered Jun 22 '12 at 04:36

Denis%20Krjuchkov's gravatar image

Denis Krjuchkov
179325

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