首页 > 解决方案 > 选择 Linq 无法将类型 byte[] 隐式转换为 System.web.httpPostedFileBase

问题描述

我正在尝试做一个 linq 选择,它连接多个表,并且表中的 1 个由字节数据类型组成。

我的 ViewModel 如下:

public class ServiceRequestsViewModel
{
    public HttpPostedFileBase Attachment { get; set; }
}

我的控制器如下:

public IEnumerable<ServiceRequestsViewModel> ServiceRequestGetAll()
    {
        var result = (from srv in DB.Services
                      join srq in DB.ServiceRequests on srv.Id equals srq.ServiceId 
                      join srp in DB.ServiceApprovers on srq.ServiceId equals srp.ServiceId
                      select new ServiceRequestsViewModel
                      {
                          Id = srq.Id,
                          ServiceId = srq.ServiceId,
                          RequestorId = srq.RequestorId,
                          ApproverId = srp.UserId,
                          Name = srv.Name,
                          Description = srq.Description,
                          Status = srq.Status,
                          Attachment = srq.Attachment,
                          CreatedBy = srq.CreatedBy,
                          CreatedDate = srq.CreatedDate
                      })
                      ;
        return result.GroupBy(x => x.Id).Select(group => group.FirstOrDefault()).OrderByDescending(a => a.Status).ThenByDescending(b => b.CreatedDate);
    }

对于 Attachment = srq.Attachment,我遇到错误:无法将类型 byte[] 隐式转换为 System.Web.HttpPostedFileBase。

标签: c#asp.net-mvc

解决方案


HttpPostedFileBase是一个包装类,因此您可以访问客户端通过此调用上传的数据。

你的电话不在那种情况下。

您的数据已经在您的数据库中。您需要选择不同的数据类型来将该数据返回给您的客户端。byte[](您已经拥有)可能适合您的情况,我们不知道,这是您的工作来决定。


推荐阅读