无法将类型System.Collections.Generic.List<>隐式转换为System.Collection

本文关键字:System 转换 Collection List 类型 Collections Generic | 更新日期: 2023-09-27 18:34:04

错误是错误"无法将类型 System.Collections.Generic.List<eToolsSystem.Entities.DTOs.CurrentOrder> 隐式转换为System.Collections.Generic.List<eToolsSystem.Entities.PurchaseOrderDetail>"

我的控制器类中的代码:

    [DataObjectMethod(DataObjectMethodType.Select, false)]
    public List<PurchaseOrderDetail> PurchaseOrdersByVendors(int VendorId)
    {
        using (var context = new ToolsContext())
        {
            var data = from item in context.PurchaseOrderDetails
                       where item.PurchaseOrder.OrderDate == null
                       && item.PurchaseOrder.VendorID == VendorId
                       select new CurrentOrder()
                       {
                           StockID = item.StockItem.StockItemID,
                           Description = item.StockItem.Description,
                           QOH = item.StockItem.QuantityOnHand,
                           ROL = item.StockItem.ReOrderLevel,
                           QOO = item.StockItem.QuantityOnOrder,
                           PoQty = item.Quantity,
                           Price = item.StockItem.PurchasePrice
                       };
            return data.ToList();
        }
    }

我的 DTO 当前订单的代码:

public class CurrentOrder
{
    public int StockID { get; set; }
    public string Description { get; set; }
    public int QOH { get; set; }
    public int ROL { get; set; }
    public int QOO { get; set; }
    public int PoQty { get; set; }
    public decimal Price { get; set; }
}

无法将类型System.Collections.Generic.List<>隐式转换为System.Collection

您正在构建一个CurrentOrder列表,但您的方法签名说它返回了一个PurchaseOrderDetail列表

将您的方法更改为:

public List<CurrentOrder> PurchaseOrdersByVendors(int VendorId)

或返回PurchaseOrderDetail列表以解决问题。 我相信后者是你想要的,基于你的方法名称。

你应该返回List<PurchaseOrderDetail>而不是List<CurrentOrder>

相关文章: