0

I need to get the id into the string with comma from the object. I used linq but the string is

System.Linq.Enumerable+WhereSelectEnumerableIterator`2[item,System.Int32]

There are my two objects:

Public Class Items
  Implements IEnumerable

   Private _ID As Integer
   Private desc As String
   Private arrItemList As New ArrayList

   Public Property ID() As Integer
    Get
        Return _ID
    End Get
    Set(ByVal Value As Integer)
        _ID = Value
    End Set
   End Property

   Public ReadOnly Property Count() As Integer
    Get
        Return arrItemList.Count
    End Get
   End Property

   Public Sub AddProduct(ByVal obj As item)
    arrItemList.Add(obj)
   End Sub

   Public Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
    Return arrItemList.GetEnumerator()
   End Function
End Class

Public Class item
  Public Property ID As Integer
  Public Property desc As String
End Class

There are my code to get the ID into the string.

Dim objItems As New Items()
objItems.AddProduct(New item With {.ID = 1, .desc = "orange"})
objItems.AddProduct(New item With {.ID = 2, .desc = "apple"})
objItems.AddProduct(New item With {.ID = 3, .desc = "peach"})

Dim query = objItems.Cast(Of item)().Select(Function(o) o.ID)
Dim result As String = String.Join(",", query)
1
  • 1
    While the question remains why you use ArrayList at all, your code should work fine. There is no need to use o.Id.ToString instead of using o.Id unless you don't use a very old version of the .NET framework. Commented Jun 16 at 13:57

1 Answer 1

1

The problem is that you are printing the query instead of result. The result is just fine (result = "1,2,3").


There are a few things that you can improve:

  • Implement the generic version IEnumerable(Of Item) instead of IEnumerable.
  • Use a List(Of Item) instead of ArrayList. It can be ReadOnly, because it is never assigned another value after initialization.
  • ID can be an auto property.
  • Rename AddProduct to Add. This together with the implementation of IEnumerable allows you to use a collection initializer in the test.
Public Class Items
    Implements IEnumerable(Of Item)

    Private ReadOnly _itemList As New List(Of Item)

    Public Property ID As Integer

    Public ReadOnly Property Count As Integer
        Get
            Return _itemList.Count
        End Get
    End Property

    Public Sub Add(obj As Item)
        _itemList.Add(obj)
    End Sub

    Public Function GetEnumerator() As IEnumerator(Of Item) Implements IEnumerable(Of Item).GetEnumerator
        Return DirectCast(_itemList, IEnumerable(Of Item)).GetEnumerator()
    End Function

    Private Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
        Return DirectCast(_itemList, IEnumerable).GetEnumerator()
    End Function
End Class
  • Use PascalCase for class names: Class Items instead of Class items
  • Add constructors for easier initialization.
Public Class Item
    Public Sub New()
    End Sub

    Public Sub New(iD As Integer, desc As String)
        Me.ID = iD
        Me.Desc = desc
    End Sub

    Public Property ID As Integer
    Public Property Desc As String
End Class

The test:

Sub Test()
    Dim objItems As New Items From {
        New Item(1, "orange"),
        New Item(2, "apple"),
        New Item(3, "peach")
    }

    Dim query = objItems.Select(Function(o) o.ID)
    Dim result As String = String.Join(",", query)
    Console.WriteLine(query) ' Output: System.Linq.Enumerable+SelectListIterator`2[VBConsole.ProductItemsClass+Item,System.Int32]
    Console.WriteLine(result) ' Output: 1,2,3
End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

@oliver, I cannot change the class. Would you tell me how to have "1,2,3" that I can set it as the value of the label. Thanks.
You don't need to change the class. I only made some suggestions for a cleaner code. "System.Linq.Enumerable+SelectListIterator2[VBConsole.ProductItemsClass+Item,System.Int32]" is the string value of query. And "1,2,3" is the value of result. You already have it right, just use the other variable. (Read again the first sentence of my answer. And also see the last two lines of my test routine.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.