views:

339

answers:

2

I have a function that works great in C# that I'm converting to VB.Net. I'm having an issue converting the result set to a generic list in VB.net.

The code:

 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

The error occurs when convertng the result via .ToList(Of Category)() The error:

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 is a simple object I've created, stored in the App_Code directory.

I have the necessary "Imports System.Collections.Generic" reference in the file so I don't see why I can't convert the result set to a generic list.

I'm pulling my hair out on this so any help would be awesome! Thanks in advance!

+3  A: 

It's saying that because you're invoking it as an extension method on an IEnumerable<Category>, the type argument has already been specified. Just get rid of the type argument:

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

That's equivalent to writing:

Dim categories = Enumerable.ToList(Of Category) _
    (From category In xmlDoc.Descendants("Table") _
     Select New Category())
Jon Skeet
Jon, there's a reason you're #1 round here, huh? :) Spot on. Simple fix and clear answer. Can't thank you enough!
Cory House
+1  A: 

The answer is simple: Replace ToList(Of Category)() with ToList().

As a side note: Maybe you want New Category(category) instead of New Category()? At the moment, your code is creating a list of empty categories...

Heinzi
Excellent point and +1 for ya. I used a C# to VB converter that stripped the section assigning values to the category object. I added that back in and all is well. Thanks!
Cory House