从物化 'System.Byte[]' 类型到可空 'System.Byte' 类型的指定转换无效

The specified cast from a materialized 'System.Byte[]' type to a nullable 'System.Byte' type is not valid

    public virtual ObjectResult<Nullable<byte>> GetPersonalPicture3(Nullable<System.Guid> contactId)
    {
        var contactIdParameter = contactId.HasValue ?
            new ObjectParameter("ContactId", contactId) :
            new ObjectParameter("ContactId", typeof(System.Guid));

        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<Nullable<byte>>("GetPersonalPicture3", contactIdParameter);
    }

在底部代码中,我遇到了这个错误"The specified cast from a materialized 'System.Byte[]' type to a nullable 'System.Byte' type is not valid."

byte x = DBPSO.GetPersonalPicture3(ProfileID).FirstOrDefault() ?? 0000;

此外,我也测试了这段代码

var x = DBPSO.GetPersonalPicture3(ProfileID).Select(B => B.Value).ToArray();

string binaryPic = System.Text.Encoding.Unicode.GetString(x);

db 上的图片可能是一个 varbinary,所以是一个字节数组,而不是单个字节。 试试这个:

public virtual ObjectResult<byte[]> GetPersonalPicture3(Nullable<System.Guid> contactId)
{
    var contactIdParameter = contactId.HasValue ?
        new ObjectParameter("ContactId", contactId) :
        new ObjectParameter("ContactId", typeof(System.Guid));

    return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<byte[]>("GetPersonalPicture3", contactIdParameter);
}