I am trying to select the data I need into simple anonymous type to serialize the 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 actually do need Secondary
to be null if the foreign key is null. How can I get an anonymous to be null straight from the select statement.
My idea solution is to be able to serialize the resulting data directly without having to process an intermediary data set.