Search code examples
c#type-conversionimplicit

Cannot implicitly convert type System.Collections.Generic.List<> to System.Collections.Generic.List<>


The error is Error "Cannot implicitly convert type System.Collections.Generic.List<eToolsSystem.Entities.DTOs.CurrentOrder> to System.Collections.Generic.List<eToolsSystem.Entities.PurchaseOrderDetail>"

The code in my controller class:

    [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();
        }
    }

The code for my 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; }
}

Solution

  • you are building a list of CurrentOrder, but your method signature says it's returning a list of PurchaseOrderDetail

    change your method to:

    public List<CurrentOrder> PurchaseOrdersByVendors(int VendorId)
    

    or return a list of PurchaseOrderDetails to fix the problem. I believe the latter is what you want, based on your method name.