Search code examples
c#entity-frameworklinqnull

Group by possible null column, can't use null propagating


Here is a left join of two tables. On the line g.rpt.Location in the group by, g.rpt could be null, so I'm getting warned about dereferencing a possible null object. ctx is a code first Entity Frameworks database.

var query = ctx.ObjTable
    .GroupJoin(ctx.RptTable, obj => obj.Upc,
        rpt => rpt.Upc,
        (obj, rpt) => new { obj, rpt })
    .SelectMany(j => j.rpt.DefaultIfEmpty(), (j, rpt) => new
        {
            j.obj, rpt,
        })
    .GroupBy(g => new
        {
            g.rpt.Location,  //Problem here
            g.obj.Category,
            g.obj.Desc,
        }
    .Select(s => new MyClass
    {
        Location = s.Key.Location,
        Category = s.Key.Category,
        Desc = s.Key.Desc,
        Total = s.Sum(x => x.rpt.Numeric),
    });

I tried g.rpt?.Location but that gives "An expression tree lambda may not contain a null propagating operator."

tried g.rpt == null ? null : g.rpt. Tried using ??.

Tried this, seemed far fetched but I tried...

.GroupBy(g => g.rpt == null 
    ? new
        {
            g.rpt.Location,
            g.obj.Category,
            g.obj.Desc,
        } 
    : new
        {
            g.obj.Category,
            g.obj.Descriptor,
        })

Solution

  • You can provide your own default value in DefaultOrEmpty, e.g.

    var defaultRpt = new Rpt {Location = "", Numeric = 0}
    
    ...
        .SelectMany(j => j.rpt.DefaultIfEmpty(defaultRpt), (j, rpt)