This is a migrated thread and some comments may be shown as answers.

Group By: Object Reference Not Set

1 Answer 70 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Joseph Roberts
Top achievements
Rank 1
Joseph Roberts asked on 12 Mar 2013, 08:50 PM
I can not get this query to execute properly through ORM.  I constantly get Object reference not set no matter how I modify the query.  Here is my latest approach, which works fine in LinqPad, but not in ORM.  

var test = from sdt in DataContext.SeasonDivisionTeams
                       join team in DataContext.Teams on sdt.TeamID equals team.Id into j1
                       from loj in j1.DefaultIfEmpty()
                       group loj by new
                       {
                           sdt.DivisionID
                       }
                           into cag
                           select new
                           {
                               cag.Key,
                               cag = from temp in cag
                                     select temp
                           };


I expect a list of items which contains a division id, and a list of teams for that division.  

Thanks,
Joe

1 Answer, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 15 Mar 2013, 03:57 PM
Hello Joe,

We have a similar known issue in our test suite. The problem seems to be in the traversal of the grouped elements within the projection. We will fix this in the future, however I am not able to provide you with a time-frame at the moment.

I would suggest you to use one of the following workarounds:

1. Select the IGrouping instances directly. Then you will be able to access the key of the group via the Key property or enumerate the instance to access the grouped items:
var test = from sdt in DataContext.SeasonDivisionTeams
       join team in DataContext.Teams on sdt.TeamID equals team.Id into j1
       from loj in j1.DefaultIfEmpty()
       group loj by new
       {
           sdt.DivisionID
       }
           into cag
           select cag;

2. If your code requires the exact same result shape, load the grouped result in memory and then do the projection to the appropriate shape:
var test = (from sdt in DataContext.SeasonDivisionTeams
       join team in DataContext.Teams on sdt.TeamID equals team.Id into j1
       from loj in j1.DefaultIfEmpty()
       group loj by new
       {
           sdt.DivisionID
       }
       into cag
       select cag).ToList().Select(x => new
                       {
                           x.Key,
                           cag = x.ToList()
                       });

I hope that helps, please excuse us for the inconvenience caused.

Regards,
Alexander
the Telerik team
OpenAccess ORM Q1 2013 is out featuring Multi-Diagrams, Persistent Data Stream Support and much more. Check out all of the latest highlights.
Tags
LINQ (LINQ specific questions)
Asked by
Joseph Roberts
Top achievements
Rank 1
Answers by
Alexander
Telerik team
Share this question
or