将LINQ转换为XML结果到VB.NET中的通用列表.奇怪的错误

Cor*_*use 2 .net linq vb.net generics linq-to-xml

我有一个在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
Run Code Online (Sandbox Code Playgroud)

通过.ToList(Of Category)()转换结果时发生错误.错误:

公共函数ToList()由于'System.Linq.Enumerable'中定义的System.Collections.Generic.List(Of TSource)'不是通用的(或者没有自由类型参数),因此不能有类型参数.

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

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

Jon*_*eet 5

它是说因为你将它作为一个扩展方法调用,所以IEnumerable<Category>已经指定了type参数.只需摆脱类型参数:

Dim categories = (From category In xmlDoc.Descendants("Table") _
                  Select New Category()).ToList()
Run Code Online (Sandbox Code Playgroud)

这相当于写作:

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