whosrdaddy
Vendor
Hi,
if want to refactor this piece of code using Linq:
these are the requirements:
- if filterRoleId is not null:
the IList<RolesDTO> WithRole must contain all RoleIds that are equal to filterRoleId and IList<RolesDTO> WithNoRole must contain all RoleIds that are not equal to filterRoleId
- same principle for filterUserId and role.UserId
the user that has the username "admin" may not appear in both lists
Thanks,
Daddy
-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
if want to refactor this piece of code using Linq:
Code:
public RolesDTO Roles(int? filterRoleId, int? filterUserId)
{
var rolesDto = new RolesDTO
{
WithRole = new List<UserRoleDTO>(),
WithNoRole = new List<UserRoleDTO>()
};
var users = repository.GetAll(false);
var roleId = filterRoleId ?? 0;
var userId = filterUserId ?? 0;
if (users != null)
{
foreach (var user in users)
{
var rolefound = user.Username.ToLowerInvariant().Equals("admin");
if (user.Roles != null && !rolefound)
{
foreach (var role in user.Roles)
{
rolefound = (roleId != 0 && role.RoleId == roleId) || (userId != 0 && role.UserId == userId);
if (rolefound)
{
rolesDto.WithRole.Add(new UserRoleDTO
{
UserId = role.UserId,
UserFullname = user.Fullname,
AccessLevel = role.AccessLevel,
RoleId = role.RoleId
});
break;
}
}
}
if (!rolefound)
rolesDto.WithNoRole.Add(new UserRoleDTO
{
UserId = user.Id,
UserFullname = user.Fullname,
});
}
}
// sort lists
return rolesDto;
}
these are the requirements:
- if filterRoleId is not null:
the IList<RolesDTO> WithRole must contain all RoleIds that are equal to filterRoleId and IList<RolesDTO> WithNoRole must contain all RoleIds that are not equal to filterRoleId
- same principle for filterUserId and role.UserId
the user that has the username "admin" may not appear in both lists
Thanks,
Daddy
-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!