在此示例中是否有返回类型化集合的本机 LINQ 方法?
这是返回类型化集合的最直接的方法吗?
抱歉重复提问。现在我正在寻找一种更好的方法...
这是关键的代码行,它返回 IEnumerable 的隐式类型,该类型用于手动循环以手动重新创建 TYPED 集合。是否有任何本机 LINQ 方法可以返回类型化排序集合,而无需重新创建集合?
Dim Sorted As ErrorProviderMessageCollection = New ErrorProviderMessageCollection(From item In MyCollection Order By item.Control.TabIndex)
Public Class ErrorProviderMessageCollection
Inherits System.Collections.ObjectModel.Collection(Of ErrorProviderMessage)
Public Sub New()
End Sub
Public Sub New(ByVal source As IEnumerable(Of ErrorProviderMessage))
Dim orderedCollection = New ErrorProviderMessageCollection()
For Each Item As ErrorProviderMessage In source
Me.Add(Item)
Next
End Sub
End Class
Public Class ErrorProviderMessage
Public Sub New(ByVal message As String, ByVal control As Control)
_Message = message
_Control = control
End Sub
Public ReadOnly Property Message() As String
Public ReadOnly Property Control() As Control
End Class
Is this the most straight forward way to return a typed collection?
Sorry for the repeated question. Now I am looking for a better way...
Here's the key line of code which returns a implicit type of IEnumerable that used to manually loop through to manually recreated a TYPED collection. Is there any native LINQ way to return a typed sorted collection without this recreating of the collection?
Dim Sorted As ErrorProviderMessageCollection = New ErrorProviderMessageCollection(From item In MyCollection Order By item.Control.TabIndex)
Public Class ErrorProviderMessageCollection
Inherits System.Collections.ObjectModel.Collection(Of ErrorProviderMessage)
Public Sub New()
End Sub
Public Sub New(ByVal source As IEnumerable(Of ErrorProviderMessage))
Dim orderedCollection = New ErrorProviderMessageCollection()
For Each Item As ErrorProviderMessage In source
Me.Add(Item)
Next
End Sub
End Class
Public Class ErrorProviderMessage
Public Sub New(ByVal message As String, ByVal control As Control)
_Message = message
_Control = control
End Sub
Public ReadOnly Property Message() As String
Public ReadOnly Property Control() As Control
End Class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关于 LINQ 需要记住的关键点是它基于延迟执行。
如果你这样做,
你实际上并没有创建一个新的集合。 LINQ 正在创建一个枚举器,根据需要对 col 中的每个项目执行。
因此,简短的回答是,不,LINQ 会生成可枚举项,它不会返回新集合(除了 ToList、ToArray 或 ToDictionary 等强制枚举的方法)。
如果你想要一个新的类型化集合,你需要强制枚举:
The key point to remember about LINQ is that it's based on deferred execution.
If you do
You AREN'T actually creating a new collection. LINQ is creating an enumerator that executes on each item in col on demand.
So, the short answer is, no, LINQ yields items in an enumerable, it doesn't return new collections (with the exception of methods like ToList or ToArray or ToDictionary which force enumeration).
If you want a new typed collection, you need to force enumeration:
不,没有 LINQ 方法可以做到这一点,因为
Collection
类不是它使用的集合类型之一。您可以将:
IEnumerable
转换为List
,它实现IList
,并且有一个List
的构造函数采用IList
的 code>CollectionNo, there is no LINQ way of doing that, as the
Collection<T>
class is not one of the collection types that it uses.You can turn the
IEnumerable<T>
into aList<T>
, which implementsIList<T>
, and there is a constructor forCollection<T>
that takes anIList<T>
: