Convert a general list to an array of strings

I have a general list (Customer). The customer class has a name, address, and phone number. I also have a property of another class that accepts an array of client names. I can do this by following these steps:

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 install:

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

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

+4
source share
1 answer

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

 Dim queryResults = From cust In customer.GetCustomers() Select cust.Name Class.CustomerNames = queryResults.ToArray() 
+7
source

Source: https://habr.com/ru/post/1342903/


All Articles