Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net select扩展方法能否将项目映射到实例化对象列表_.net_Vb.net_Extension Methods - Fatal编程技术网

.net select扩展方法能否将项目映射到实例化对象列表

.net select扩展方法能否将项目映射到实例化对象列表,.net,vb.net,extension-methods,.net,Vb.net,Extension Methods,我有两份清单声明如下: Dim lstDBItems As New List(Of DBItem) Dim lstAppItems As New List(Of AppItem) 我正在尝试这样做: Return lstDBItems.Select(Function(x) dim oItem As New AppItem() oItem.Property1 = x.DbPropert

我有两份清单声明如下:

Dim lstDBItems As New List(Of DBItem)
Dim lstAppItems As New List(Of AppItem)
我正在尝试这样做:

Return lstDBItems.Select(Function(x)
                            dim oItem As New AppItem()
                            oItem.Property1 = x.DbProperty1
                            '...
                            Return oItem
                         End Function).ToList()
我有一个函数返回(AppItem的)列表:

在上述函数中,我填充lstDBItems,然后编写返回语句,如下所示:

Return lstDBItems.Select(Function(x)
                            dim oItem As New AppItem()
                            oItem.Property1 = x.DbProperty1
                            '...
                            Return oItem
                        End Function)
奇怪的是,代码是编译的,但在一段时间内,我得到了一个类型大小写错误。做我想做的事情的正确方法是什么

请原谅篡改屏幕截图

选择后添加
ToList()
,将
列表(AppItem)
作为返回值

代码不应编译为初始值。检查您是否有严格的选项

一旦您了解了为什么它不应该编译,您可以选择:

  • 在查询结束时调用,如下所示:

    Return lstDBItems.Select(Function(x)
                                dim oItem As New AppItem()
                                oItem.Property1 = x.DbProperty1
                                '...
                                Return oItem
                             End Function).ToList()
    
  • 将返回类型更改为
    IEnumerable(属于AppItem)


选择
方法的结果属于
IEnumerable(属于AppItem)
类型,不能分配给
列表类型的变量(属于AppItem

如果您有选项Strict On,则不会编译此文件