5

I have a generic List(Of Customer). The customer class has a name, address, and phone number properties. I also have a property of another class that accepts a customer name array. I am able to do this by doing the following:


Dim names As String()
Dim i As Integer = 0
'customer.GetCustomers is a List(of Customer)
For Each customer As Customer In customer.GetCustomers()
     ReDim Preserve names(i)
     names(i) = customer.Name
     i += 1
Next

Then to set it:


'CustomerNames is a String()
Class.CustomerNames = names

Is there a better way to convert this to a string array? Any help is appreciated. Thanks.

1 Answer 1

8

You can use LINQ (Pardon my VB, I prefer C#)

Dim queryResults = From cust In customer.GetCustomers() Select cust.Name
Class.CustomerNames = queryResults.ToArray()
Sign up to request clarification or add additional context in comments.

1 Comment

This is what I was looking for! I was able to change it to: Class.CustomerNames = (From cust In customer.GetCustomers() Select cust.Name).ToArray Thanks for your help.

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.