在此示例中是否有返回类型化集合的本机 LINQ 方法?

发布于 2024-11-01 20:18:18 字数 1103 浏览 0 评论 0原文

这是返回类型化集合的最直接的方法吗?

抱歉重复提问。现在我正在寻找一种更好的方法...

这是关键的代码行,它返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纸短情长 2024-11-08 20:18:18

关于 LINQ 需要记住的关键点是它基于延迟执行。

如果你这样做,

Dim y = col.Where(Function(i) i.SomeProp = True)

你实际上并没有创建一个新的集合。 LINQ 正在创建一个枚举器,根据需要对 col 中的每个项目执行。

因此,简短的回答是,不,LINQ 会生成可枚举项,它不会返回新集合(除了 ToList、ToArray 或 ToDictionary 等强制枚举的方法)。

如果你想要一个新的类型化集合,你需要强制枚举:

Dim col2 = New ErrorProviderMessageCollection(col.Where(Function(i) i.SomeProp = True))

The key point to remember about LINQ is that it's based on deferred execution.

If you do

Dim y = col.Where(Function(i) i.SomeProp = True)

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:

Dim col2 = New ErrorProviderMessageCollection(col.Where(Function(i) i.SomeProp = True))
谁的年少不轻狂 2024-11-08 20:18:18

不,没有 LINQ 方法可以做到这一点,因为 Collection 类不是它使用的集合类型之一。

您可以将 IEnumerable 转换为 List,它实现 IList,并且有一个 List 的构造函数采用 IList 的 code>Collection

Dim Sorted As Collection<ErrorProviderMessage> = _
  New Collection<ErrorProviderMessage>( _
    (From item In MyCollection Order By item.Control.TabIndex).ToList() _
  )

No, 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 a List<T>, which implements IList<T>, and there is a constructor for Collection<T> that takes an IList<T>:

Dim Sorted As Collection<ErrorProviderMessage> = _
  New Collection<ErrorProviderMessage>( _
    (From item In MyCollection Order By item.Control.TabIndex).ToList() _
  )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文