Hi,

I've discovered a serious bug : those queries don't return the same result:

var groupedSumQueryable = from l in Query.All<MyEntity>().First().Lines
                                    group l by l.Rate.GetValueOrDefault() into g
                                    select new { Rate = g.Key, BaseAmount = g.Sum(l => l.Amount.GetValueOrDefault()) };

var groupedSumList = from l in Query.All<MyEntity>().First().Lines.ToList()
                               group l by l.Rate.GetValueOrDefault() into g
                               select new { Rate = g.Key, BaseAmount = g.Sum(l => l.Amount.GetValueOrDefault()) };

The only difference is the ToList converting the IQueryable to a simple list.

Sample project: [attachment=0:ie0iu504]DoTestSum.zip[/attachment:ie0iu504]

Regards,


Updated at 26.04.2010 15:31:58

Thanks for your input : but it's not the real problem here.

I will put below what I posted in the attached file:

First, I create some entities:

var helloWorld = new MyEntity
          {
            Text = "Hello World!",
            Lines =
            {
              new Line { Rate = 5.5M, Amount = 2M},
              new Line { Rate = 5.5M, Amount = 2M},
              new Line { Rate = 5.5M, Amount = 2M},
              new Line { Rate = 5.5M, Amount = 2M},
              new Line { Rate = 5.5M, Amount = 2M},
              new Line { Rate = 5.5M, Amount = 2M}
            }
          }

Then the first query (that could be quite logically included in a global ToList() as you suggest)

var groupedSumList = from l in Query.All<MyEntity>().First().Lines
                               group l by l.Rate.GetValueOrDefault() into g
                               select new { Rate = g.Key, BaseAmount = g.Sum(l => l.Amount.GetValueOrDefault()) };

          foreach (var groupedSum in groupedSumList)
          {
            Console.WriteLine(groupedSum.Rate + " Sum= " + groupedSum.BaseAmount);
          }

Output is: 5,500000000000 Sum= 33,000000000000

Then I execute the first query (indeed this will translate in sql like this : SELECT * FROM Lines WHERE ...)

var groupedSumQueryable = from l in Query.All<MyEntity>().First().Lines.ToList()
                                    group l by l.Rate.GetValueOrDefault() into g
                                    select new { Rate = g.Key, BaseAmount = g.Sum(l => l.Amount.GetValueOrDefault()) };

          foreach (var groupedSum in groupedSumQueryable)
          {
            Console.WriteLine(groupedSum.Rate + " Sum= " + groupedSum.BaseAmount);
          }

Output is : 5,500000000000 Sum= 12,000000000000

I will let you guess which one is correct ;)

This thread was imported from our support forum. The original discussion may contain more detailed answer.

asked Apr 26 '10 at 11:18

olorin's gravatar image

olorin
358848792


One Answer:

Dmitri Maximov (Xtensive) wrote:

Hello olorin,

thanks for the sample. We'll start the investigation shortly.

I've opened the following issue: http://code.google.com/p/dataobjectsdot ... ail?id=641


psulek wrote:

I think i know what is wrong in this query:

from l in Query.All<MyEntity>().First().Lines.ToList()

calling ToList() is meaning as final projection, means that items from first of MyEntity object, all Lines objects are copied to generic list and on this list is made rest of conditions (group, sum, ...). And if i am right this is good and "by design". I think you can rewrite it to this:

var groupedSumQueryable = (from l in Query.All<MyEntity>().First().Lines
                                    group l by l.Rate.GetValueOrDefault() into g
                                    select new { Rate = g.Key, BaseAmount = g.Sum(l => l.Amount.GetValueOrDefault()) }).ToList();

to have generic list as an result of query.

answered Apr 26 '10 at 11:35

Editor's gravatar image

Editor
46154156157

It seems that this is definitely a bug. To be fixed.

(Apr 26 '10 at 11:35) Dmitri Maximov Dmitri%20Maximov'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