Group By: Object Reference Not Set

2 posts, 0 answers
  1. Joseph Roberts
    Joseph Roberts avatar
    47 posts
    Member since:
    Jul 2004

    Posted 12 Mar 2013 Link to this post

    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
  2. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 15 Mar 2013 Link to this post

    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.
  3. DevCraft banner
Back to Top