【发布时间】:2013-07-23 01:46:32
【问题描述】:
我正在尝试将我需要的数据选择为简单的匿名类型来序列化 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 为空。如何直接从 select 语句中获取匿名为空。
我的想法解决方案是能够直接序列化结果数据,而无需处理中间数据集。
【问题讨论】:
-
看看这里...问题在于 null stackoverflow.com/questions/682429/…
-
在我看来像个骗子
-
此问题与您链接的其他帖子无关
-
所以你没有空问题?如果您检查并确定您的问题不相关。很好。
-
可以选择
p.SecondaryId.HasValue ? new { Name = p.Secondary.Name } : new { Name = (string)null }吗?
标签: .net linq entity-framework