如何解决“无法将类型System.Collections.Generic.List <>隐式转换为System.Collections.Generic.List <>”

时间:2019-06-15 15:16:50

标签: c# linq model-view-controller .net-core

谁能告诉我为什么我无法在GetWorkOrdersByLocation中返回workOrder?这是我的控制器:

public IActionResult Index()
{
    return View();
}

public IActionResult Privacy()
{
    return View();
}

public IActionResult WorkOrders()
{
    GetWorkOrders();
    return View(); 
}

public IActionResult GetWorkOrders()
{
    List<WorkOrder> workOrders = new List<WorkOrder> ();

    workOrders.Add(new WorkOrder { UserId = 1, LocationId = 
"Philadelphia", Date = DateTime.Now, Reason = "Lights", IsActive = 
true });

    workOrders.Add(new WorkOrder { UserId = 2, LocationId = "Camden", 
Date = DateTime.MinValue, Reason = "Plumbing", IsActive = true });

    workOrders.Add(new WorkOrder { UserId = 3, LocationId = 
"Burlington", Date = DateTime.Now, Reason = "Water", IsActive = 
false });

    workOrders.Add(new WorkOrder { UserId = 4, LocationId = 
"Wilmington", Date = DateTime.MaxValue, Reason = "Lights", IsActive 
= true }); return View(workOrders); 

}

public IActionResult GetWorkOrdersByLocation()
{
    List<WorkOrder> workOrders = new List<WorkOrder>();

    workOrders = workOrders.Select(x => x.LocationId).ToList();

    return workOrders; 

}

我在return语句return workOrders上遇到错误。我不确定为什么这行不通。我从模型类WorkOrder中创建列表WorkOrder的新实例。现在,我试图列出所有位置。

2 个答案:

答案 0 :(得分:2)

您的代码有几个问题:

  1. GetWorkOrdersByLocation中,您定义了List<WorkOrder> workOrders并尝试为其分配一个List<string>,因为LocationId是字符串类型。
  2. GetWorkOrdersByLocation中,您只有一个空的workOrders列表,因此对其进行的任何选择也将返回空。
  3. WorkOrders似乎与GetWorkOrdersByLocation类似,但是创建了一个View而没有提供任何数据,因为尽管您调用GetWorkOrdersByLocation并不会对其返回的内容做任何事情。
  4. 次要(?),GetWorkOrdersByLocation的命名与您返回工作订单位置而不是按位置返回的工作订单(已排序/分组?)时的名称似乎并没有真正的匹配。

通常,您需要一种提供数据的方法。 GetWorkOrders似乎可以这样做,但是您返回一个View,而不是数据。此方法应仅返回您的列表,然后由WorkOrdersGetWorkOrdersByLocation使用。

public IActionResult WorkOrders()
{
    // get the work order list and return a view on it
    return View(GetWorkOrders()); 
}

private List<WorkOrder> GetWorkOrders()
{
    List<WorkOrder> workOrders = new List<WorkOrder> ();

    workOrders.Add(new WorkOrder { UserId = 1, LocationId = 
"Philadelphia", Date = DateTime.Now, Reason = "Lights", IsActive = 
true });

    workOrders.Add(new WorkOrder { UserId = 2, LocationId = "Camden", 
Date = DateTime.MinValue, Reason = "Plumbing", IsActive = true });

    workOrders.Add(new WorkOrder { UserId = 3, LocationId = 
"Burlington", Date = DateTime.Now, Reason = "Water", IsActive = 
false });

    workOrders.Add(new WorkOrder { UserId = 4, LocationId = 
"Wilmington", Date = DateTime.MaxValue, Reason = "Lights", IsActive 
= true });

 // return the work orders to be used by your view methods
 return workOrders; 
}

public IActionResult GetWorkOrdersByLocation()
{
    // get your work order list and extract all locations
    var locations = GetWorkOrders().Select(x => x.LocationId).ToList();

    // return view on all locations
    return View(locations); 
}

如果您希望最后一种方法返回按位置排序的所有工作单的视图,请执行以下操作:

public IActionResult GetWorkOrdersByLocation()
{
    // get your work order list and sort by location
    var sortedByLocation = GetWorkOrders().OrderBy(x => x.LocationId).ToList();

    // return view on all sorted work orders
    return View(sortedByLocation); 
}

答案 1 :(得分:1)

GetWorkOrdersByLocation()实际上从任何地方都没有得到任何workOrders,它只有一个空的List<WorkOrder>();-也许叫某个东西(私人的?),它从某个地方得到一些东西,然后{{1 }}我从示例中删除了一些代码,只专注于请求。

return View (workOrders);

评论中的问题:“成为列表”是private List<WorkOrder> CreateWorkOrders() { List<WorkOrder> workOrders = new List<WorkOrder> (); workOrders.Add(new WorkOrder { UserId = 1, LocationId = "Philadelphia", Date = DateTime.Now, Reason = "Lights", IsActive = true }); workOrders.Add(new WorkOrder { UserId = 2, LocationId = "Camden", Date = DateTime.MinValue, Reason = "Plumbing", IsActive = true }); workOrders.Add(new WorkOrder { UserId = 3, LocationId = "Burlington", Date = DateTime.Now, Reason = "Water", IsActive = false }); workOrders.Add(new WorkOrder { UserId = 4, LocationId = "Wilmington", Date = DateTime.MaxValue, Reason = "Lights", IsActive = true }); return workOrders; } public IActionResult GetWorkOrdersByLocation(string locationid) { var workOrders CreateWorkOrders(); locationWorkOrders = workOrders.Where(x => x.LocationId == locationid).ToList(); return View(locationWorkOrders); } 在这种情况下是一种方法。您可能还会为您的位置创建一个类,因为这些位置通常是带有ID,短名称(如州缩写),长名称,描述等的单独对象,并带有适当的命名属性。