Reputation: 196439
i have an array of Tag objects
class Tag
{
public string Name;
public string Parent;
}
i want code to return a list of the tag names as an array of strings
Upvotes: 3
Views: 311
Reputation: 2472
To best use IEnumerable
interface. Otherwise you can use linq queries for that or basic foreach loop
Upvotes: 0
Reputation: 31548
How about simply:
var tags = new List<Tag> {
new Tag("1", "A"),
new Tag("2", "B"),
new Tag("3", "C"),
};
List<string> names = tags.ConvertAll(t => t.Name);
No Linq needed, and if you need an array, call ToArray()
.
Upvotes: 6
Reputation: 81711
I assume that you want something like this :
public List<string> GetNamesOfTag(List<Tag> tags)
{
List<string> Name = new List<string>();
foreach(Tag item in tags)
{
Name.Add(item.name);
}
returns Name;
}
Upvotes: 0
Reputation: 11957
var names = from t in tags
select t.Name;
Something like this will give you an IEnumerable over names, just use .ToArray()
if you wan't array of those.
Upvotes: 5