I know that I can use FOR EACH to easily accomplish my goal below, however I run into this scenario a LOT and was wondering if there was a more elegant solution.
Class Foo
Public X as String
Public Y as Integer
End Class
Dim c As New List(Of Foo) = GetFooItemsFromDatabase......
I want to get a comma delimited string of all the X’s in c. If I had a simple array of string called simpleArray I could do something like
dim s as String = String.Join(", ", simpleArray)
Is there a way of building simpleArray (LINQ, built in functions, etc) that will accomplish this instead of having to use FOR EACH and build it myself?
EDIT: It seems LINQ is the way to go. However converting the many suggestions from C# to VB yields the following which the compiler does not like.
Dim s As String = c.[Select](Function(myVal) myValue.X)
OK this is C#, so apologies, but you could use a little LINQ:
The
Selectextension method allows you to project the output, you are basically taking an enumerable ofcand projecting it to an enumerable ofX. You then callToArrayto translate yourIEnumerable<string>into astring[](in C# vernacular).LINQ is fully supported in VB.NET and is well worth reading more into:
http://msdn.microsoft.com/en-us/library/bb763068.aspx
If you are new to it, the standard naming of “LINQ” tends to cover both the new syntax:
And also its guise in extension methods:
A VB.NET LINQ source:
http://www.wrox.com/WileyCDA/Section/LINQ-Extension-Methods-with-Visual-Basic-2008.id-310907.html