I am trying to select the data I need for a simple anonymous type to serialize data for a Json request.
using (var dbContext = new DataContext()) { var vals = dbContext.Primaries.Select(p => new { Name = p.Name, Secondary = p.SecondaryId.HasValue ? new { Name = p.Secondary.Name } : null }); }
but when I call the enumerator on vals, I get the following exception
Unable to create a null constant value of type 'Anonymous type'. Only entity types, enumeration types or primitive types are supported in this context.
I really need Secondary be null if the foreign key is null. How can I get the anonymous null value directly from the select statement.
My solution is to be able to serialize the resulting data directly without processing an intermediate data set.
source share