Reputation: 3930
I have a list of objects, one of the properties on the object is a string.
Is there a way to use Join
and Linq create a delimited list of the string property in one line of code? If not, what is the least amount of code to accomplish this?
EXAMPLE:
public class MyObject
{
public int MyInt { get; set; }
public string MyString { get; set; }
}
I want to create a string using Join
that contains a delimited list of all the MyString
values in MyObject
.
Something like this -
List<MyObject> myObjectList = GetMyObjectList(); //Contains a list of MyObject
//I want to do something like this
string MyListOfMyStrings = String.Join(",", myObjectList.ForEach(x => x.Mystring));
MyListOfMyStrings will be set to a delimited list of the values of MyString for all of the objects inside myObjectList
Upvotes: 0
Views: 214
Reputation: 383
You want to replace where you are using ForEach with a linq statement like below:
string MyListOfMyStrings = String.Join(",", (from myObject in myObjectList select myObject.MyString).ToArray());
See the MSDN guide for linq
Upvotes: 1