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 a List<clsTranslation> from the IEnumerable<clsTranslation>.
(I'd also advise you to ditch the cls prefix; it goes against the .NET naming conventions.)
That would leave:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With