InvalidCastExeption in LINQ to XML

I use LINQ to XML to get some data from resources, and here I have a function where I take some values ​​and then create a new object (Type Translation), so when I call the function, I get the translation object. To date, I have this code:

     Public Shared Function RetrieveTranslation(ByVal file As String) As List(Of clsTranslation)
    Dim valuetrans = From vl In XElement.Load(file).Elements("data") Select (New clsTranslation With {.Filename = file, .Value = vl.Element("value").Value, .TranslationId = vl.Attribute("name").Value})
    Return valuetrans
End Function

The problem is that with this code I got this error: Unable to pass an object like "WhereSelectEnumerableIterator 2[System.Xml.Linq.XElement,clsTranslation]' to type 'System.Collections.Generic.List1 [clsTranslation]".

Do you know how to quit it? Thanks in advance,

Alfonso.

+3
source share
1 answer

If you compiled using the Strict on option, you would have found it at compile time.

- ToList() , List<clsTranslation> IEnumerable<clsTranslation>.

( cls, .NET.)

:

Public Shared Function RetrieveTranslation(ByVal file As String) _
        As List(Of Translation)
    Return (From vl In XElement.Load(file).Elements("data") _
            Select (New Translation With { _
                               .Filename = file, _
                               .Value = vl.Element("value").Value, _
                               .TranslationId = vl.Attribute("name").Value}) _
           ).ToList()
End Function
+2

All Articles