I've found a bug when doing a group by, I've reduced my faulty query to a really small query, and can still reproduce the problem. This is the query:

@foreach (var tmp in (
        from t in Query.All<Line>()
        group t by t
            into t3
            select new
                Test = t3.Key,
                Test2 = t3.Key.ItemVersionsUntyped.Count()
        Entity: @tmp.Test.Id<br />
        EntitySet (SQL): @tmp.Test2<br /> <!--This will return 0, which is wrong -->
        EntitySet (LINQ): @tmp.Test.ItemVersionsUntyped.Count() <!-- This will return 1, which is correct -->

The model is quite large, in code, but it's like this, more or less (omitting [Field] etc. The special ItemItemVersionsEntitySet is just to enforce some special constraints when adding to the entityset):

class ItemItemVersionsEntitySet : EntitySet<IItemVersionBase>
abstract class ItemBase : Entity, IItemBase {}
abstract class ItemVersionBase : Entity, IItemVersionBase {}
abstract class Item<TOwner, TChild> : ItemBase
    [Association(PairTo = "ItemUntyped", OnOwnerRemove = OnRemoveAction.Cascade, OnTargetRemove = OnRemoveAction.Clear)]
    ItemItemVersionsEntitySet ItemVersionsUntyped {get; private set;}
abstract class ItemVersion<TOwner, TChild> : ItemVersionBase
    IItemBase ItemUntyped { get; set; }
class Line : Item<Line, LineVersion>
    //Some fields
class LineVersion : ItemVersion<Line, LineVersion>
    //Some fields

All entities share this: Guid Id {get; private set;} [Field] on everything Line and LineVersion are HierarchyRoot, with SingleTable and IncludeTypeId

In DB they are represented as only two tables, Line and LineVersion.

There are some other fields in the models. and other classes, but they are not relevant. The generated SQL looks like this, after some beautifying. Ignore the fact that there are a big list of unions, it's irrelevant, normally I wouldn't query ItemVersionsUntyped, I would use ItemVersionsTyped which is a LinqRewriter to get only LineVersion and not every IItemVersionBase. However, to keep the query as simple as possible I avoided the helpers.

SELECT [a].[id], 
       [b].[stations.id]                                            AS [#a.Stations.Id], 
       [b].[stations.typeid]                                        AS [#a.Stations.TypeId], 
       (SELECT Count_big(*) 
        FROM   ((SELECT [c].[id], 
                 FROM   (SELECT [d].[id], 
                         FROM   [dbo].[bodml.lineversion] [d]) [c]) 
                (SELECT [e].[id], 
                 FROM   (SELECT [f].[id], 
                         FROM   [dbo].[bodml.linestationversion] [f]) [e]) 
                (SELECT [g].[id], 
             FROM   (SELECT [h].[id], 
                     FROM   [dbo].[bodml.stationversion] [h]) [g]) 
            (SELECT [i].[id], 
             FROM   (SELECT [j].[id], 
                     FROM   [dbo].[bodms.userversion] [j]) [i]) 
            (SELECT [k].[id], 
             FROM   (SELECT [l].[id], 
                     FROM   [dbo].[bodms.passwordversion] [l]) [k]) 
            (SELECT [m].[id], 
             FROM   (SELECT [n].[id], 
                     FROM   [dbo].[itemsetitemversion(bodms.user-bodms.baserole)] [n]) [m]) 
            (SELECT [o].[id], 
             FROM   (SELECT [p].[id], 
                     FROM   [dbo].[itemsetitemversion(bodml.line-bodml.linestation)] [p]) [o])) [q] 
    WHERE  ( ( [q].[itemuntyped.id] = [q].[id] ) 
             AND ( [q].[itemuntyped.typeid] = [q].[typeid] ) )) AS [c01umn] 
    FROM   (SELECT [r].[id], 
    FROM   [dbo].[bodml.line] [r] 
    GROUP  BY [r].[id], 
              [r].[typeid]) [a] 
   INNER JOIN [dbo].[bodml.line] [b] 
           ON ( ( [a].[id] = [b].[id] ) 
                AND ( [a].[typeid] = [b].[typeid] ) )

As you can see it compares the wrong tables here:

    WHERE  ( ( [q].[itemuntyped.id] = [q].[id] ) 
             AND ( [q].[itemuntyped.typeid] = [q].[typeid] ) )) AS [c01umn]

I think it should be like this instead:

    WHERE  ( ( [q].[itemuntyped.id] = [a].[id] ) 
             AND ( [q].[itemuntyped.typeid] = [a].[typeid] ) )) AS [c01umn]

When executing the original query in sql server management I get this result, which I didn't expect:

D8296E94-FA01-437D-9EBD-77B4F3DB02D5    100 1E60CE0C-7ED4-4626-A3DA-0FBFDB71A9CD    111 0
346ED63A-D8E7-4EA4-A9B5-A505E343371C    100 D9E936B6-1CCD-496F-B42F-9142F2003FC8    111 0

However, when executing my modified sql I get this result, which I expected:

D8296E94-FA01-437D-9EBD-77B4F3DB02D5    100 1E60CE0C-7ED4-4626-A3DA-0FBFDB71A9CD    111 1
346ED63A-D8E7-4EA4-A9B5-A505E343371C    100 D9E936B6-1CCD-496F-B42F-9142F2003FC8    111 1

I tried different queries, and they all seem to share the problem that setting a property to .Key in the grouping works, but using .Key.Anything() won't work.

If I try this query instead (with .Count instead of .Count()) I get an error message instead:

from t in Query.All<Line>()
            group t by t
                into t3
                select new
                    Test = t3.Key,
                    Test2 = t3.Key.ItemVersionsUntyped.Count


[IndexOutOfRangeException: Index was outside the bounds of the array.]
   Xtensive.Orm.Linq.Rewriters.EntitySetAccessRewriter.VisitMemberAccess(MemberExpression m) +283
   Xtensive.Linq.ExpressionVisitor`1.Visit(Expression e) +658
   Xtensive.Linq.ExpressionVisitor.VisitExpressionList(ReadOnlyCollection`1 expressions) +170
   Xtensive.Linq.ExpressionVisitor.VisitNew(NewExpression n) +81
   Xtensive.Linq.ExpressionVisitor`1.Visit(Expression e) +891
   Xtensive.Linq.ExpressionVisitor.VisitLambda(LambdaExpression l) +81
   Xtensive.Linq.ExpressionVisitor`1.Visit(Expression e) +814
   Xtensive.Linq.ExpressionVisitor.VisitUnary(UnaryExpression u) +81
   Xtensive.Linq.ExpressionVisitor`1.Visit(Expression e) +227
   Xtensive.Linq.ExpressionVisitor.VisitExpressionList(ReadOnlyCollection`1 expressions) +170
   Xtensive.Linq.ExpressionVisitor.VisitMethodCall(MethodCallExpression mc) +128
   Xtensive.Orm.Linq.Rewriters.EntitySetAccessRewriter.VisitMethodCall(MethodCallExpression mc) +101
   Xtensive.Linq.ExpressionVisitor`1.Visit(Expression e) +736
   Xtensive.Orm.Linq.Rewriters.EntitySetAccessRewriter.Rewrite(Expression e) +66
   Xtensive.Orm.Linq.TranslatorContext..ctor(Session session, CompilerConfiguration rseCompilerConfiguration, Expression query) +417
   Xtensive.Orm.Linq.QueryProvider.Translate(Expression expression, CompilerConfiguration compilerConfiguration) +104

[QueryTranslationException: Unable to translate 'Query.All().GroupBy(t => t).Select(t3 => new @<Test, Test2>(
))' expression. See inner exception for details.]
   Xtensive.Orm.Linq.QueryProvider.Translate(Expression expression, CompilerConfiguration compilerConfiguration) +274
   Xtensive.Orm.Linq.QueryProvider.Translate(Expression expression) +113
   Xtensive.Orm.Linq.QueryProvider.Execute(Expression expression) +147
   Xtensive.Orm.Linq.Queryable`1.GetEnumerator() +124

(The need to use the Key is simply to limit the amount of SQL queries, prefetch etc. For performance, simply)

I hope you will be able to reproduce this easily, if not, I will try to make a smaller model to reproduce this on :)

DO.NET 4.6.2 (From NuGet) SQL Server 2005 (If relevant)

asked Jan 29 '13 at 08:30

Onkelborg's gravatar image


Thanks for the report, we'll check it very soon.

(Jan 30 '13 at 10:38) Denis Krjuchkov Denis%20Krjuchkov's gravatar image

One Answer:

Confirmed and fixed. Fix would be available in DataObjects.Net 4.5.7 and 4.6.3

answered Feb 01 '13 at 06:40

Denis%20Krjuchkov's gravatar image

Denis Krjuchkov

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


Once you sign in you will be able to subscribe for any updates here



Asked: Jan 29 '13 at 08:30

Seen: 4,610 times

Last updated: Feb 01 '13 at 06:40

powered by OSQA