Jsonリクエストのデータをシリアル化するために、必要なデータを単純な匿名型に選択しようとしています。
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
});
}
しかし、vals で列挙子を呼び出すと、次の例外が発生します
Unable to create a null constant value of type 'Anonymous type'. Only entity types, enumeration types or primitive types are supported in this context.
Secondary
外部キーがnullの場合、実際にはnullである必要があります。select ステートメントから直接匿名を null にする方法を教えてください。
私のアイデアの解決策は、中間データセットを処理することなく、結果のデータを直接シリアル化できるようにすることです。