I am using LINQ to XML to take some data from resources , and here I have a function, where I take some values and then I create a new object (Type Translation), so when I call the function I get a Translation object. By now 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 cast object of type ‘WhereSelectEnumerableIterator2[System.Xml.Linq.XElement,clsTranslation]' to type 'System.Collections.Generic.List1[clsTranslation]’.
Do you know the way to cast it?
Thanks in advance,
Alfonso.
If you were compiling with Option Strict on, you’d have found this out at compile time.
You don’t cast it – you call
ToList()on the result, to create aList<clsTranslation>from theIEnumerable<clsTranslation>.(I’d also advise you to ditch the
clsprefix; it goes against the .NET naming conventions.)That would leave: