Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Error when retrieving associated users using a join table and Microsoft Identity

$
0
0

I've facing an error that I am unsure of how to solve. I am trying to retrieve a list of users associated to an organization via a one to many join table. The error being thrown is:

Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)

I cant seem to find a work around to this (and I'm teaching myself to code so my knowledge is limited).

Is anyone able to provide some direction on how to resolve this? The error itself is in the OrganizationsRepository on the 3rd last line:

return orgUsers;

IOrganizationsRepository:

using System.Collections.Generic;
using System.Threading.Tasks;
using Outmatch.API.Models;

namespace Outmatch.API.Data
{
    public interface IOrganizationRepository
    {
        void Add<T>(T entity) where T: class;
        void Delete<T>(T entity) where T: class;
        Task<bool> SaveAll();
        Task<IEnumerable<Organizations>> GetOrganizations();
        Task<Organizations> GetOrganization(int id);
        Task<OrgToLoc> GetOwnees(int OrganizationId, int LocationId);

        // Begin Help on many to many 
        // Get users attached to an organization
        Task<IEnumerable<User>> GetOrgUsers(int organizationId);
        // Get locations attached to an organization
        Task<IEnumerable<Organizations>> GetOrganizationNameById(int LocationId);
        // End many to many help
    }
}

OrganizationsRepository:

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Outmatch.API.Helpers;
using Outmatch.API.Models;

namespace Outmatch.API.Data
{
    public class OrganizationsRepository : IOrganizationRepository
    {
        private readonly DataContext _context;
        public OrganizationsRepository(DataContext context)
        {
            _context = context;

        }
        public void Add<T>(T entity) where T : class
        {
            _context.Add(entity);
        }

        public void Delete<T>(T entity) where T : class
        {
            _context.Remove(entity);
        }

        // get an organization only one of them).  Pass in the organization ID from the user interface, and pull the organization that corresponds to the 
        // id, returning it to the GUI 
        public async Task<Organizations> GetOrganization(int id)
        {
            var organization = await _context.Organization.FirstOrDefaultAsync(u => u.Id == id);
            return organization;

        }

        // Get the list of all organizations and return them to the GUI
        public async Task<IEnumerable<Organizations>> GetOrganizations()
        {
            var organizations = await _context.Organization.ToListAsync();
            return organizations;
        }

        public async Task<OrgToLoc> GetOwnees(int OrganizationId, int LocationId)
        {
            return await _context.LocationOwners.FirstOrDefaultAsync(u => u.OwnerId == OrganizationId && u.OwneeId == LocationId);
        }

        // Begin help many to many 
        public async Task<IEnumerable<Organizations>> GetOrganizationNameById(int LocationId)
        {
            var OrganizationName = await _context.Locations
                .Include(l => l.Owners)
                .Where(o => o.Id == LocationId)
                .SelectMany(l => l.Owners.Select(on => on.Owner))
                .ToListAsync();

            return OrganizationName;
        }
        // End help many to many 

        public async Task<bool> SaveAll()
        {
            return await _context.SaveChangesAsync() > 0;
        }

        public async Task<IEnumerable<User>> GetOrgUsers(int orgId)
    {
        var orgUsers = await _context.Organization
            .Include(u => u.UserId)
            .Where(u => u.Id == orgId)
            .SelectMany(u => u.UserId.Select(ui => ui.OrganizationId))
            .ToListAsync();

        return orgUsers;
    }
    }
}

UsersModel: 

using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Identity;

namespace Outmatch.API.Models
{
    // List of properties for the User (Client) table in the db
    public class User : IdentityUser<int>
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime ActiveDate { get; set; }
        public DateTime EndDate { get; set; }

        // User Roles Management
        public virtual ICollection<UserRole> UserRoles { get; set; }

        // Organization to Client table ties
        public ICollection<OrgToClients> OrganizationId { get; set; }
    }
}

OrgToClients Model: 

namespace Outmatch.API.Models
{
    public class OrgToClients
    {
        public int OrganizationId { get; set; }
        public int UserId { get; set; }
        public User User { get; set; }
        public Organizations Organization { get; set; }
    }
}

Any assistance would be greatly appreciated.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>