.net 在 VB 中创建匿名类型列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1034075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:54:21  来源:igfitidea点击:

Creating a list of Anonymous Type in VB

.netvb.netlinqlistanonymous-types

提问by Daniel

I'd like to create a list of an anonymous type, for example:

我想创建一个匿名类型的列表,例如:

Dim lineItem = New With {.Name = myFile(index).Last_Name & ", " & myFile(index).First_Name, _
                         .StartDate = myFile(index).Day,
                         .EndDate = myFile(index).Day}

I have created that anonymous type. Now I'd like to add it to a list of that type. How do I declare a list of that type?

我已经创建了那个匿名类型。现在我想将它添加到该类型的列表中。如何声明该类型的列表?

采纳答案by JaredPar

Here's a handy method for creating a list of an anonymous type from a single anonymous type.

这是从单个匿名类型创建匿名类型列表的方便方法。

Public Function CreateListFromSingle(Of T)(ByVal p1 As T) As List(Of T)
  Dim list As New List(Of T)
  list.Add(p1)
  return List
End Function

Now you can just do the following

现在您只需执行以下操作

Dim list = CreateListFromSingle(dsResource)

EDITOP wanted a way to create the list before creating an element.

编辑OP 想要一种在创建元素之前创建列表的方法。

There are 2 ways to do this. You can use the following code to create an empty list. It borders on hacky because you are passing parameters you don't ever intend to use but it works.

有两种方法可以做到这一点。您可以使用以下代码创建一个空列表。它与 hacky 接壤,因为您正在传递您不打算使用但它有效的参数。

  Public Function CreateEmptyList(Of T)(ByVal unused As T) As List(Of T)
    Return New List(Of T)()
  End Function

  Dim x = CreateEmptyList(New With { .Name = String.Empty, .ID = 42 })

回答by CleverPatrick

Here is how to do it inline using casting by example (without creating a second function):

以下是如何使用按示例进行内联转换(不创建第二个函数):

Sub Main()
    Dim x = New With {.Name = "Bob", .Number = 8675309}
    Dim xList = {x}.ToList()
End Sub

(based on c# version posted here)

(基于此处发布的 c# 版本)

Essentially you create the anonymous type, put it in an array ( {x} ) then use the array's .ToList() method to get a list.

本质上,您创建匿名类型,将其放入数组 ( {x} ) 中,然后使用数组的 .ToList() 方法获取列表。

回答by iQueue

How about a single line construct like this?

像这样的单行结构怎么样?

Dim yourList = {(New With {.Name="", .Age=0})}.Take(0).ToList

回答by Dario

Since the type is anonymous, you must use generic and type-inference.

由于类型是匿名的,您必须使用泛型和类型推断。

The best way is to introduce a generic function that creates an empty collection from a prototype object.

最好的方法是引入一个从原型对象创建一个空集合的通用函数。

Module Module1

    Sub Main()
        Dim dsResource = New With {.Name = "Foo"}

        Dim List = dsResource.CreateTypedList
    End Sub

    <System.Runtime.CompilerServices.Extension()> _
    Function CreateTypedList(Of T)(ByVal Prototype As T) As List(Of T)
        Return New List(Of T)()
    End Function

End Module

回答by Konrad Rudolph

I like Jared's solution but just to show the direct equivalent of Jon's code (notwithstanding my comment there):

我喜欢 Jared 的解决方案,但只是为了展示 Jon 代码的直接等价物(尽管我在那里发表了评论):

Public Function EnumerateFromSingle(Of T)(ByVal p1 As T) As IEnumerable(Of T)
    Return New T() { p1 }
End Function

Not very useful by itself, since it's not extensible … but may be used to seed other LINQ magic for creating larger lists.

本身不是很有用,因为它不可扩展……但可用于为其他 LINQ 魔法播种以创建更大的列表。

回答by pepe

In a single line you can add anonymous types into a list

在一行中,您可以将匿名类型添加到列表中

Dim aList = {(New With {.Name="John", .Age=30}), (New With {.Name="Julie", .Age=32})}.ToList

回答by just.another.programmer

If you're adding values to the list by iteration, I assume you have an IEnumerableto start with? Just use .Selectwith .ToList. That's what it's designed for.

如果您通过迭代向列表中添加值,我假设您有一个IEnumerable开始?只需.Select.ToList. 这就是它的设计目的。

Dim l = dsResources.Select(Function(d) New With {
                              .Name = d.Last_Name & ", " & d.First_Name
                          }).ToList()