从数组中获取值并添加到一个字符串中

use*_*193 -1 .net c#

对象Book具有类型的Author属性.Namestring

我想迭代所有的Authors并将它的Name字符串添加到用逗号分隔的一个字符串(不是数组),所以这个字符串应该在as

string authorNames = "Author One, Author two, Author three";
Run Code Online (Sandbox Code Playgroud)
string authorNames = string.Empty;
foreach(string item in book.Authors)
{
    string fetch = item.Name;
    ??
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*ten 6

您可以将该string.Join功能与LINQ一起使用

string authorNames = string.Join(", ", book.Authors.Select(a => a.Name));
Run Code Online (Sandbox Code Playgroud)