DO version 5.0.6

Model:

[HierarchyRoot]
[Serializable]
public class TestEntity : Entity
{
    [Key]
    [Field(Nullable = false)]
    public Guid Id { get; private set; }

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

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

Test:

    var dc = new DomainConfiguration("sqlserver://localhost/DO40-Tests");

    dc.Types.Register(typeof(TestEntity));

    dc.UpgradeMode = DomainUpgradeMode.Recreate;
    var sessionConfiguration = new SessionConfiguration(SessionOptions.AutoActivation | SessionOptions.ServerProfile);

    using (var domain = Domain.Build(dc))
    {
        using (var session = domain.OpenSession(sessionConfiguration))
        using (session.Activate())
        using (var t = session.OpenTransaction())
        {
            new TestEntity { TestField = "Test1", Sum = 1};
            new TestEntity { TestField = "Test2", Sum = 1 };
            new TestEntity { TestField = "Test3", Sum = 1 };
            new TestEntity { TestField = "Test3", Sum = 1 };

            t.Complete();
        }

        using (var session = domain.OpenSession(sessionConfiguration))
        using (session.Activate())
        using (var t = session.OpenTransaction())
        {
            Assert.AreEqual(0, Session.Current.Query.All<TestEntity>()
                .Select(e => e.TestField.Contains("3") ? e.Sum : -e.Sum)
                .Sum(e => e)); //OK

            Assert.AreEqual(0, Session.Current.Query.All<TestEntity>()
                .Select(e => e.TestField.Contains("3") ? e.Sum : -e.Sum)
                .Sum()); // NOT OK

            t.Complete();
        }
    }

Exception:

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

Additional information: Unable to translate 'Query.All().Select(e => e.TestField.Contains("3")

? e.Sum

: -(e.Sum)).Sum()' expression. See inner exception for details.

Unable to translate 'All().Select(e => IIF(e.TestField.Contains(\"3\"), e.Sum, -e.Sum)).Sum()' expression. Aggregates for non primitive types are not supported.

----- Edited 24.07.2015

This work

Session.Current.Query.All<TestEntity>()
                    .ToArray()
                    .Select(e => e.TestField.Contains("3") ? e.Sum : -e.Sum)
                    .Sum();

asked Jul 13 '15 at 08:55

Anton%20Guschin's gravatar image

Anton Guschin
73303035

edited Jul 24 '15 at 07:22

Hi Anton. Thanks for report. I will check it and answer you about results

(Jul 13 '15 at 10:15) Alexey Kulakov Alexey%20Kulakov's gravatar image

One Answer:

Hello Anton

You should specify column for types which isn't primitive. System.Linq.Sum<TSource>() has same behavior - if there is no override method for source type then you should specify lambda-expression. I think it is correct.

Update! I've checked a bit closer. DO sometimes rewrites expression with Sum(selector) with Select(selector).Sum(). In some cases DO already have Select(selector).Sum() so in these cases we shouldn't generate exception. I've already added this case to issue list. Thanks for the report.

answered Jul 20 '15 at 05:24

Alexey%20Kulakov's gravatar image

Alexey Kulakov
77225

edited Jul 29 '15 at 04:58

Hm, not understand completely

After .Select(e => e.TestField.Contains("3") ? e.Sum : -e.Sum) it is IQueryable<decimal> (can be checked in debug)

If we use ToArray(), exception doesn't thrown

Maybe bug?)

(Jul 24 '15 at 07:28) Anton Guschin Anton%20Guschin's gravatar image

Hello Anton.

I've checked this behavior again and updated my answer. Thanks for the report.

(Jul 29 '15 at 05:00) Alexey Kulakov Alexey%20Kulakov's gravatar image

Great, thx

(Jul 29 '15 at 09:32) Anton Guschin Anton%20Guschin's gravatar image
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