我有一个在 C# 中运行良好的函数,我正在将其转换为 VB.NET。我在将结果集转换为 VB.NET 中的通用列表时遇到问题。

代码:

    Public Function GetCategories() As List(Of Category) 
        Dim xmlDoc As XDocument = XDocument.Load("http://my_xml_api_url.com") 
        Dim categories = (From category In xmlDoc.Descendants("Table") _ 
        Select New Category()).ToList(Of Category)() 
 
        Return categories 
    End Function 

通过.ToList(Of Category)() 转换结果时出现错误。错误:

Public Function ToList() As System.Collections.Generic.List(Of TSource)' defined in 'System.Linq.Enumerable' is not generic (or has no free type parameters) and so cannot have type arguments.

Category 是我创建的一个简单对象,存储在 App_Code 目录中。

我在文件中有必要的“Imports System.Collections.Generic”引用,所以我不明白为什么我不能将结果集转换为通用列表。

请您参考如下方法:

这是说因为你在 IEnumerable<Category> 上将它作为扩展方法调用, 类型参数已经指定。只需去掉类型参数:

Dim categories = (From category In xmlDoc.Descendants("Table") _ 
                  Select New Category()).ToList() 

这相当于写:

Dim categories = Enumerable.ToList(Of Category) _ 
    (From category In xmlDoc.Descendants("Table") _ 
     Select New Category()) 


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!