Need More Info
Last Updated: 18 Sep 2025 09:37 by Alexander
Alexander
Created on: 16 Sep 2025 12:57
Category: Grid
Type: Bug Report
0
Multi level grouping with ExpandoObject causes NullReferenceException if top level group has null key

I'm using an OnRead grid with ExpandoObjects. The error happens on the backend when trying to process the result of ToDataSourceResult(...)

When trying to access the Items property of AggregateFunctionsGroup in a multi level grouping scenario, using ExpandoObject, and the top level group has a null key, trying to access the Items property will result in a NullReferenceException. I've made this helper, but it cannot process the subgroups because of this error.

private static void FlattenGroup<T>(AggregateFunctionsGroup group, List<T> result)
{
    if (group == null)
    {
        return;
    }

    if (group.HasSubgroups)
    {
        foreach (var sub in group.Items.OfType<AggregateFunctionsGroup>())
        {
            FlattenGroup(sub, result);
        }
    }
    else
    {
        result.AddRange(group.Items.OfType<T>());
    }
}

In a scenario where I have an ExpandoObject with properties A and B, where A = 1 and B = null; grouping by A then B works. Grouping by A or B alone also works. But grouping by B then A causes the NullReferenceException when trying to access the group.Items. 

2 comments
Alexander
Posted on: 18 Sep 2025 09:37

Hi Tsvetomir,

Thanks for your reply. I did also find some similar posts about grouping and null key issues. However, they all seemed to be using LoadGroupsOnDemand, which we do not in our application. We do use OnRead and we cannot avoid ExpandoObjects. Additionally, the way the exception is thrown is not catchable by making a null check like you propose. The null reference is not thrown until you try to enumerate the collection. The same exception is also thrown when trying to enumerate the SubGroups property.

I don't think the REPL example is worth much, because its behaviour is different from the exact same code when I run it locally. In the REPL I can't even group by the non-null column. Running this locally produces the error, but in a slightly different way, than when the error occours on the backend. If possible, I recommend trying this locally, setting a breakpoint at the loop in FlattenGroup, then inspecting the Items and SubGroups property of the null group REPL

The problems with the workaround for us is that a null value is a valuable piece of information, because it essentially means the users have not yet decided about the value of that particular field, on that particular row. The problem with setting a placeholder value, is that we have multiple data types where anything but null will give users a wrong impression of what the data is. For instance, using a default value for a nullable int, would be 0, and would give false information to users.

We also cannot tell users that they cannot use grouping on columns where there might be null values - it's essential to the users that they can group on everything.

Here's a little snippet of how the error occours.

 

 

 

ADMIN
Tsvetomir
Posted on: 18 Sep 2025 07:09

Hi Alexander,

This scenario is a known limitation with grouping by nullable properties in ExpandoObject data, especially when using LoadGroupsOnDemand or OnRead. The following public feedback item is relevant:

Helper Method Modifications

To prevent the NullReferenceException, you can add a null check for group.Items. However, this does not resolve the underlying grouping logic issue - groups with a null key may not contain the expected items due to the limitations described above.

private static void FlattenGroup<T>(AggregateFunctionsGroup group, List<T> result)
{
    if (group == null || group.Items == null)
    {
        return;
    }
    if (group.HasSubgroups)
    {
        foreach (var sub in group.Items.OfType<AggregateFunctionsGroup>())
        {
            FlattenGroup(sub, result);
        }
    }
    else
    {
        result.AddRange(group.Items.OfType<T>());
    }
}

This modification will prevent the exception, but may skip groups with null keys entirely.

Possible Workarounds

  • Preprocess Data -  Before grouping, replace null values with a placeholder (e.g., "No Value") to avoid null keys.
  • Grouping Strategy - Try to avoid grouping by columns that may have null values if you must use ExpandoObject.

If the above information and result do not align with your expectations, please provide a runnable example that showcase the encountered issue. You can use our REPL platform to share the reproduction example.

I look forward you reply.

Regards,
Tsvetomir
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.