Anyone know of a good source for converting a LINQ list into a strong typed list
for example I have a simple class, which I wish to convert the datatable contents into using LINQ. I have the following code so far which I can convert into an anonymous list
Dim test = (From t As DataRow In ds.Tables("Test").AsEnumerable _
Select New With {.countryID = t.Field(Of Integer)(0), .country = t.Field(Of String)(1)}).ToList
Which works as expected but it is a anonymous typed list but now I want to convert it to a strong typed list using the class below (in the code block), but I get the following Error
dim x as a list(of countries) = test
Value of type 'System.Collections.Generic.List(Of <anonymous type="">)' cannot be converted to 'System.Collections.Generic.List(Of WindowsApplication1.Countries)'
I have also tried
Dim test as IEnumerable(of Countries) = (From t As DataRow In ds.Tables("Test").AsEnumerable _
Select New With {.countryID = t.Field(Of Integer)(0), .country = t.Field(Of String)(1)}).ToList
But get the following error.
Unable to cast object of type 'System.Collections.Generic.List`1[VB$AnonymousType_0`2[System.Int32,System.String]]' to type 'System.Collections.Generic.IEnumerable`1[WindowsApplication1.Countrys]'.
public class Countries
private _CountryId as integer
private _Country as name
Public Property CountryID() As Integer
Get
Return _CountryID
End Get
Set(ByVal value As Integer)
_CountryID = value
End Set
End Property
Public Property Country() As String
Get
Return _Country
End Get
Set(ByVal value As String)
_Country = value
End Set
End Property
end class
Thanks
Simon
FOUND A SOLUTION!
Dim test As IEnumerable(Of Countrys) = (From t In dt.Tables("Test").AsEnumerable _
Select New Countrys With {.CountryID = t.Field(Of Integer)(0), .Country = t.Field(Of String)(1)}).ToList
now on to the next stage to allow a method to do this for any table / list of objects