我有以下代码:
Public Shared Function GetAvailableManufacturers() As List(Of Manufacturer)
'first get all the live orders and extract their mfrs'
Dim sos As List(Of OrderForm) = GetFormsByStatus(StockStatus.Building)
Dim unavailableMfrs As New List(Of Manufacturer)
For Each so As StockingOrder In sos
unavailableMfrs.Add(so.Source)
Next
'then remove all mfrs with an open SO from a list of all mfrs'
Dim allMfrs As List(Of Manufacturer) = Manufacturer.GetManufacturers
Return allMfrs.Except(unavailableMfrs) <----- error here
End Function
对上面的作用的解释:
GetFormsByStatus()
不言自明GetManufacturers()
返回数据库中所有制造商的列表 我获取可用制造商的想法是在我的开放表格中获取所有制造商的列表,然后获取所有制造商的列表并排除第一个列表中的所有制造商,如下所示(伪):
List A: {1,2,3,4,5,6,7,8,9,10}
List B: {5,7,10}
Result: {1,2,3,4,6,8,9}
我已经设置了我的
Manufacturer
根据 this article 分类以便进行比较,但我仍然收到此错误:Unable to cast object of type '
<ExceptIterator>d__92'1[csCore.Manufacturer]'
to type 'System.Collections.Generic.List'1[csCore.Manufacturer]
'.
我一开始以为是因为在测试过程中
GetFormsByStatus()
返回 0 结果可能是造成问题的原因,但 Except()
没有意义如果提供的列表有 0 个项目,则不起作用。谁能发现我做错了什么?非常感谢!
最佳答案
该函数需要 List(Of Manufacturer) 返回类型,而不是 IEnumerable(Of Manufacturer),因此将 .ToList() 添加到返回值应该可以修复它:
Return allMfrs.Except(unavailableMfrs).ToList()
关于asp.net - .Except<T> 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2485329/