mjmendes mjmendes -4 years ago 66
C# Question

LINQ iqueryable to IEnumerable

Im trying to get data from 3 tables using LINQ to return an IEnumerable but not having much luck, when i want to return the result it is of type IQueryable and im not sure what i should be doing in order to achieve my goal

public Task<IEnumerable<PortalUser>> GetUsers(string accountCode)
{
var res = from u in _db.Users
join ur in _db.UserRoles on u.ContactGuid equals ur.ContactGuid
join r in _db.Roles on ur.RoleId equals r.Id
where ur.AccountCode == accountCode
select(new PortalUser(u.Id,u.FriendlyName,u.UserName,r.RoleName));
return res;
}


In my select i try to use the PortalUser constructor to make PortalUsers and thought this would give me what i wanted but clearly not.

public class PortalUser
{
public PortalUser(int id,string friendlyName,string username,string rolename)
{
Id = id.ToString();
Firstname = friendlyName.Split(' ')[0];
Lastname = friendlyName.Split(' ')[1];
Username = username;
Role = rolename;
}

public string Id { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Username { get; set; }
public string Role { get; set; }
}



Cannot implicitly convert type
'System.Linq.IQueryable<BSAR.Services.Models.Outbound.Full.PortalUser>'
to
'System.Threading.Tasks.TaskSystem.Collections.Generic.IEnumerable<BSAR.Services.Models.Outbound.Full.PortalUser'>
. An explicit conversion exists (are you missing a cast?)

Answer Source

GetUsers doesn't return a task - therefore you have to change the return type to IEnumerable<PortalUser>:

public IEnumerable<PortalUser> GetUsers(string accountCode)
{
    var res = from u in _db.Users
        join ur in _db.UserRoles on u.ContactGuid equals ur.ContactGuid
        join r in _db.Roles on ur.RoleId equals r.Id
        where ur.AccountCode == accountCode
        select (new PortalUser(u.Id, u.FriendlyName, u.UserName, r.RoleName));
    return res;
}

If you want a Task however you could do something like this:

public Task<IEnumerable<PortalUser>> GetUsers(string accountCode)
{
    return new Task<IEnumerable<PortalUser>>(() =>
    {
        var res = from u in _db.Users
            join ur in _db.UserRoles on u.ContactGuid equals ur.ContactGuid
            join r in _db.Roles on ur.RoleId equals r.Id
            where ur.AccountCode == accountCode
            select (new PortalUser(u.Id, u.FriendlyName, u.UserName, r.RoleName));
        return res;
    });
}

To give the task a use you could make the method async like this:

public async Task<IEnumerable<PortalUser>> GetUsers(string accountCode)
{
    return await new Task<IEnumerable<PortalUser>>(() =>
    {
        var res = from u in _db.Users
            join ur in _db.UserRoles on u.ContactGuid equals ur.ContactGuid
            join r in _db.Roles on ur.RoleId equals r.Id
            where ur.AccountCode == accountCode
            select (new PortalUser(u.Id, u.FriendlyName, u.UserName, r.RoleName));
        return res;
    });
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download