[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
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
:)
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
Re: [RESOLVED] How to loop through anonymous type array
or you can do it the way you discovered lol
Re: [RESOLVED] How to loop through anonymous type array