score:0

you can get around this by always returning the anonymous where the anonymous object may have null properties.

using (var dbcontext = new datacontext())
{
    var vals = dbcontext.primaries.select(p => new
    {
       name = p.name,
       secondary = new { name = p.secondaryid.hasvalue ? p.secondary.name : null }
    });
}

and if you really want to make secondary null if p.secondaryid is null you can add the following.

//tolist allows you to work with linq to objects rather than linq to entities.  
//however, you'll generally want to call tolist() last for performance reasons.
var valscleaned = vals.tolist()
                      .select(v => { 
                                       if(v.secondary.name == null)
                                       {
                                           v.secondary == null;
                                       }
                                       return v;
                                   });

score:0

i don't have a "solution" per se. to workaround this issue, i simply projected the entire secondary entity. i'm not happy with this solution.

using (var dbcontext = new datacontext())
{
    var vals = dbcontext.primaries.select(p => new
    {
       name = p.name,
       secondary = p.secondary
    });
}

obviously, projecting an entire entity is akin to "select *" - which is a bad practice. and it may not work for you, depending on your actual query.


Related Query

More Query from same tag