FSou1 FSou1 - 18 days ago 6
C# Question

Conversion between IEnumerable to IList (Dapper return result)

There is signature of

UserManager
method (ASP .NET Identity Core default implementation):

public Task<IList<string>> GetRolesAsync(User user) {
const string query = @"
select r.[Name]
from [UserRoles] ur
left join [Roles] r on r.RoleId = ur.RoleId
where ur.UserId = @userId;
";

return Task.Factory.StartNew(() => {
using (SqlConnection connection = new SqlConnection(_connectionString))
return connection.Query<string>(query, new { userId = user.UserId });
});
}


But unfortunately
Dapper.Query<T>
returns
IEnumerable<T>
.

Is it possible to return
IList<T>
or i have to make conversion myself?

Answer

Try to use an explicit cast

return (IList<string>)connection.Query<string>(query, new { userId = user.UserId });
Comments