从实体框架设置linq中的字节数组到实体

时间:2018-04-04 22:27:04

标签: c# entity-framework linq linq-to-entities

我在我的linq实体查询中得到一个例外

的异常
  

LINQ表达式节点类型' NewArrayBounds' LINQ to Entities

不支持

当我尝试获取' RegisteredInstructorImage'

以下是我为解决错误所做的工作

.Select(i => new EventResult
            {
                IsInstructorRegistered = i.RegisteredInstructor == null ? false : true,
                RegisteredInstructorId = i.RegisteredInstructor != null ? i.RegisteredInstructor.YogaProfileId : 0,
                RegisteredInstructorName = i.RegisteredInstructor != null ? i.RegisteredInstructor.FirstName : "",
                RegisteredInstructorImage = i.RegisteredInstructor != null ? i.RegisteredInstructor.PrimaryImage11 : new byte[0],
                RegisteredInstructorTotalReviews = i.RegisteredInstructor != null ? i.RegisteredInstructor.TotalReviews : 0,

            }).ToPagedList(Page, 10);

EventResult中,我根据此SO post按照以下方式对其进行初始化,但这并未影响我的结果,我仍然得到错误。我在这里做错了吗?

public class EventResult
{
    public EventResult()
    {
        RegisteredInstructorImage = new byte[] { 0 };
    }
    public byte[] RegisteredInstructorImage { get; set; }
}

1 个答案:

答案 0 :(得分:0)

我建议改变:

i.RegisteredInstructor != null ? i.RegisteredInstructor.PrimaryImage11 : new byte[0]

为:

i.RegisteredInstructor?.PrimaryImage11

然后考虑更改RegisteredInstructorImage的属性设置器,以便在需要时检查null(并更改为空数组)。