Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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

VB
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

VB
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]'.

VB
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!
VB
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
Posted
Updated 7-Feb-11 5:04am
v3

I would change the Select method to create a list of Countries directly. In C# it would look like this (I'm sure you'll be able to convert it to VB):

C#
List<Countries> test = (....).Select(t => new Countries() {CountyID = ..., Country = ...}).ToList()


Basically, instead of creating an anonymous object inside the Select statement, create an object of type Countries.
 
Share this answer
 
Typically this is done with a call to Cast<>(). I don't know the VB syntax to call it, so check this page out:

http://msdn.microsoft.com/en-us/library/bb341406.aspx[^]
 
Share this answer
 
Comments
Simon_Whale 7-Feb-11 11:02am    
Nish thanks for your pointer it gave me something to go on. Solved the problem a different way though.

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
Nish Nishant 7-Feb-11 12:34pm    
Glad to hear you resolved it.
Espen Harlinn 7-Feb-11 14:39pm    
Good link
Sergey Alexandrovich Kryukov 7-Feb-11 20:18pm    
Good stuff, my 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900