IList <string>由String.Join转换为字符串

时间:2019-02-17 18:41:31

标签: c#

我有一个列表IList,我想加入所有字符串,我使用该代码,但结果是:

Roles = String.Join(",",_userManager.GetRolesAsync(user)).

System.Runtime.CompilerServices.AsyncTaskMethodBuilder 1+AsyncStateMachineBox 1 [System.Collections.Generic.IList 1[System.String],Microsoft.AspNetCore.Identity.UserManager 1 + d__112

我通过以下代码对其进行了修复:

var roles = await _userManager.GetRolesAsync(user);

var role = "";

foreach (var r in roles)
{
    if (!role.Contains(",")) { role = r; }
    else { role = "," + r; }
}
vm.ApplicationUser = user;
vm.Roles = role;

我想知道为什么第一个代码不起作用。

1 个答案:

答案 0 :(得分:2)

Roles = String.Join(",", await _userManager.GetRolesAsync(user))