I have an array of Tag objects
class Tag { public string Name; public string Parent; }
I want the code to return a list of tag names as an array of strings
var names = from t in tags select t.Name;
Something like this will give you IEnumerable over the names, just use .ToArray()it if you are not an array of them.
.ToArray()
How about just:
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);
There is no need for Linq, and if you need an array, call ToArray().
ToArray()
IEnumerable. linq foreach
IEnumerable
return (from Tag in MyTagArray select Tag.Name).ToArray();
string[] tagArray = (from t in tagList select t.Name).ToArray();
, - :
public List<string> GetNamesOfTag(List<Tag> tags) { List<string> Name = new List<string>(); foreach(Tag item in tags) { Name.Add(item.name); } returns Name; }
Source: https://habr.com/ru/post/1716964/More articles:How to avoid a series of "if" statements? - if-statementWhat are the basic knowledge requirements for a Perl developer? - perlDisplay XML file with XSL on an existing web page - htmlDisplaying database query data in the form of a table - c #Setting the Dump class - objective-cC # .NET is a remote path to a directory or file - c #Convert NSString with a number to the appropriate localization format for a phone number - objective-cWhy is my regex matching version number not working? - regexA standalone GTK application when there is no X window environment - linuxHow to create a url to open a page using the jQuery lightbox plugin color window? - jqueryAll Articles