需要一种如何“将匿名列表linq到强类型列表"的方法. [英] Need a how to "linq anonymous list to a strong typed list"

查看:46
本文介绍了需要一种如何“将匿名列表linq到强类型列表"的方法.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道将LINQ列表转换为强类型列表的好方法

例如,我有一个简单的类,希望将数据表的内容转换为使用LINQ.到目前为止,我拥有以下代码,可以将其转换为匿名列表

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



类型``System.Collections.Generic.List(Of< anonymous type =">))''的值不能转换为``System.Collections.Generic.List(Of WindowsApplication1.Countries)'' >
我也尝试过



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



但是出现以下错误.

无法转换类型为"System.Collections.Generic.List`1 [VB $ AnonymousType_0`2 [System.Int32,System.String]]"的对象来类型为"System.Collections.Generic.IEnumerable`1 [WindowsApplication1 .Countrys]".



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

推荐答案

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.


这篇关于需要一种如何“将匿名列表linq到强类型列表"的方法.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆