且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

需要一种如何“将匿名列表linq到强类型列表"的方法.

更新时间:2022-11-27 08:23:06

AnonymousType_0`2 [System.Int32,System.String]] ''以键入``System.Collections.Generic.IEnumerable`1 [WindowsApplication1.Countrys]''.

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


通常,这是通过调用Cast<>()完成的.我不知道如何调用VB语法,因此请查看以下页面:

http://msdn.microsoft.com/en-us/library/bb341406.aspx [ ^ ]
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[^]


我将更改Select方法以直接创建国家列表.在C#中,它看起来像这样(我确定您可以将其转换为VB):

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):

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



基本上,不是在Select语句内创建匿名对象,而是创建类型为Countries的对象.



Basically, instead of creating an anonymous object inside the Select statement, create an object of type Countries.