无法隐式转换类型System.Collections.Generic.List<>到System.Collections.Generic.List<>

时间:2014-12-12 02:54:55

标签: c# type-conversion implicit

错误是错误“无法将类型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 CurrentOrder的代码:

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; }
}

2 个答案:

答案 0 :(得分:1)

您正在构建CurrentOrder的列表,但您的方法签名表明它已返回PurchaseOrderDetail的列表

将您的方法更改为:

public List<CurrentOrder> PurchaseOrdersByVendors(int VendorId)

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

答案 1 :(得分:0)

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