【发布时间】:2015-03-12 13:16:20
【问题描述】:
有人知道为什么我在尝试通过 ForestLogisticBEAN 类编辑数据库中的某些内容时收到此错误吗?
不能隐式转换类型 'System.Collections.Generic.List 到 'System.Linq.IQueryable'。 存在显式转换(您是否缺少演员表?)
这里是集成层:
public void EditParcelDetail(ForestLogisticBEAN parcel){
IQueryable<ForestLogisticBEAN> _ForestLogisticsBeans;
_ForestLogisticsBeans = (from order in _context.Orders
from tracking in _context.Trackings
where order.CustomerID == parcel.Id
where tracking.CustomerId == parcel.Id
select new {order.DeliveryDate, order.OrderDate, tracking.Status} ).ToList();
}
这是 ForestLogisticBEAN 类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ForestLogistic.Data.BEANS
{
public class ForestLogisticBEAN
{
public int TrackingId { get; set; }
public int OrderId { get; set; }
public bool Status { get; set; }
public DateTime OrderDate { get; set; }
public DateTime DeliveryDate { get; set; }
public ForestLogisticBEAN() { }
}
}
更新: 我将其更改为 List 而不是 IQueryable 但现在它正在抛出此错误
传入字典的模型项是类型 'System.Collections.Generic.List`1[ForestLogistics.Data.BEANS.ForestLogisticBEAN]', 但是这本字典需要一个类型的模型项 'ForestLogistics.Data.Order'。
这是新的集成层:
public void EditParcelDetail(ForestLogisticBEAN parcel)
{
List<ForestLogisticBEAN> _ForestLogisticsBeans;
_ForestLogisticsBeans = (from order in _context.Orders
from tracking in _context.Trackings
where order.CustomerID == parcel.Id
where tracking.CustomerId == parcel.Id
select new ForestLogisticBEAN {
DeliveryDate = parcel.DeliveryDate,
OrderDate = parcel.OrderDate,
Status = parcel.Status
});
_context.SaveChanges();
}
目的是能够将订单和跟踪表与bean类结合起来
【问题讨论】:
-
为什么声明为
IQueryable<T>而不是List<T>或IEnumerable<T>? -
为什么不使用var?
var _ForestLogisticsBeans = (from order in _context.Orders....
标签: c# asp.net-mvc