Results 1 to 5 of 5

Thread: [RESOLVED] How to loop through anonymous type array

  1. #1

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Resolved [RESOLVED] How to loop through anonymous type array

    Hi Guys,

    I'm trying to get distinct rowsfrom a datatable using LINQ.

    Code:
      Dim categories = dt.AsEnumerable().[Select](Function(row) New With { _
                     Key .Id = row.Field(Of UInt32)("ID"), _
                     Key .Name = row.Field(Of String)("name") _
                 }).Distinct().ToArray
    This does the job. I'm not sure how to loop through the categories array now to get the ID and Name values. Please advise me

  2. #2

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: How to loop through anonymous type array

    no worries. I got it.

    For Each cat In categories
    Dim catID As UInt32 = cat.Id
    Dim catname As String = cat.Name
    Next

  3. #3
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: [RESOLVED] How to loop through anonymous type array

    I think you need to create a structure and use a function. i am assuming you are getting employee information so i use the name employee for this example, you can follow the idea and use a more appropriate name

    Public Structure Employee
    Dim id as integer
    Dim name as String
    End Structure

    Then use a function

    Public Function GetEmployeeInfo() as Employee()
    Dim categories = dt.AsEnumerable().[Select](Function(row) New With { _
    Key .Id = row.Field(Of UInt32)("ID"), _
    Key .Name = row.Field(Of String)("name") _
    }).Distinct().ToArray
    Return categories
    End function

    then you can use

    employee.id
    employee.name

    Hope this helps

  4. #4
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: [RESOLVED] How to loop through anonymous type array

    or you can do it the way you discovered lol

  5. #5

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: [RESOLVED] How to loop through anonymous type array

    thanks anyway billboy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width