Return Iqueryable in the method

Asked

Viewed 40 times

1

I am using Identityframework in my project and need to create a method that returns a Iqueryable

Follows below class structure:

namespace MeuProjeto.Controllers
    public class UsuariosController : Controller
    {
        private readonly SignInManager<IdentityUser> _signInManager;
        private readonly UserManager<IdentityUser> _userManager;
        private readonly RoleManager<IdentityRole> _roleManager;
        private readonly AppSettings _appSettings;

        public UsuariosController(SignInManager<IdentityUser> signInManager,
                                  UserManager<IdentityUser> userManager,
                                  RoleManager<IdentityRole> roleManager,
                                  IOptions<AppSettings> appSettings)
        {
            _signInManager = signInManager;
            _userManager = userManager;
            _roleManager = roleManager;
            _appSettings = appSettings.Value;
        }

        public IQueryable<Usuario> ObterUsuarios1()
        {
            return _userManager.Users
                    .Select(u => new Usuario() { Id = u.Id, Name = u.UserName, Email = u.Email, Claims = ???? })
                    .OrderBy(u => u.Name);
        }

        public async Task<IQueryable<Usuario>> ObterUsuarios2()
        {
            var lista = new List<Usuario>();
            var usuarios = _userManager.Users.ToList();
            foreach (var usuario in usuarios)
            {
                var claims = (await _userManager.GetClaimsAsync(usuario)).Select(c => new UsuarioClaim { Type = c.Type, Value = c.Value });
                var novoUsuario = new Usuario() { Id = usuario.Id, Name = u.UserName, Email = usuario.Email, Claims = claims };
                lista.Add(novoUsuario);
            }

            return lista.AsQueryable();
        }
    }
}

User and User Structureclaim:

public class Usuario
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public IEnumerable<UsuarioClaim> Claims { get; set; }
}

public class UsuarioClaim
{
    public string Value { get; set; }
    public string Type { get; set; }
}

I need to make one of the methods work.

  • In the Method Get users I couldn’t add the Claims, but is returning Iqueryable correctly.
  • In the method Get users2 I managed to add the Claims, but is giving an error when I try to use the return.

Error message using Get Users2 return (Asqueryable):

System.InvalidOperationException: The provider for the source IQueryable doesn't implement IAsyncQueryProvider. Only providers that implement IAsyncQueryProvider can be used for Entity Framework asynchronous operations.

1 answer

0

Have you tried the code below?

 public IQueryable<Usuario> ObterUsuarios()
        {
            return _userManager.Users
                    .Include(u => u.UsuarioClaim)
                    .OrderBy(u => u.Name);
        }
  • Thanks for the tip, Shai Pinto! But for me is giving error in include. I understand that _userManager.Users would be from Identityuser where there is no User claim.

  • Does the error not refer to the return of the method to be a Task. Try: Return Task.Fromresult<Iqueryable<Usuario>(list.Asqueryable())

Browser other questions tagged

You are not signed in. Login or sign up in order to post.