Results 1 to 5 of 5

Thread: [RESOLVED] LINQ for converting IList<Role> to string array of Role.Name

  1. #1

    Thread Starter
    Hyperactive Member Krokonoster's Avatar
    Join Date
    Jan 2010
    Location
    Cape Town
    Posts
    448

    Resolved [RESOLVED] LINQ for converting IList<Role> to string array of Role.Name

    Hi,
    The following work, but I'm pretty sure can be converted to a LINQ query, and been wasting time on it (Not necessary, but curiosity can be a killer sometimes)
    (User got a member of type IList<Role> where Role have a member "RoleName")

    Code:
    public override string[] GetRolesForUser(string username)
    {
        var roles = new StringBuilder();
        try
        {
            User user = UserRepository.GetUserByName(username, ApplicationName);
            if (user != null)
            {
                foreach (Role role in user.Roles)
                {
                    roles.Append(role.RoleName + ",");
                }
            }
        }
        catch (Exception ex)
        {
            throw new MemberAccessException("Error processing role data - " + ex.Message);
        }
        if (roles.Length > 0)
        {
            roles.Remove(roles.Length - 1, 1);
            return roles.ToString().Split(',');
        }
        return new string[0];
    }
    Figured where I loop over the roles a query can do the job......no?


  2. #2
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: LINQ for converting IList<Role> to string array of Role.Name

    Take the IList<Role> and use the Select in a lambda where you retrieve the .RoleName.

    Code:
    string[] NameArray = RList.Select(r => r.RoleName).ToArray();
    RList as the IList<Role>
    Last edited by AceInfinity; Jul 8th, 2012 at 08:11 PM.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  3. #3

    Thread Starter
    Hyperactive Member Krokonoster's Avatar
    Join Date
    Jan 2010
    Location
    Cape Town
    Posts
    448

    Re: LINQ for converting IList<Role> to string array of Role.Name

    Awesome, thanks!
    Did not test it yet, but much shorter / more readable:
    Code:
    public override string[] GetRolesForUser(string username)
    {
        try
        {
            User user = UserRepository.GetUserByName(username, ApplicationName);
            if (user != null)
                return user.Roles.Select(r => r.RoleName).ToArray();
        }
        catch (Exception ex)
        {
            throw new MemberAccessException("Error processing role data - " + ex.Message);
        }
        return new string[0];
    }


  4. #4
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: [RESOLVED] LINQ for converting IList<Role> to string array of Role.Name

    Enjoy, hope it works for what you're trying to do here
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  5. #5

    Thread Starter
    Hyperactive Member Krokonoster's Avatar
    Join Date
    Jan 2010
    Location
    Cape Town
    Posts
    448

    Re: [RESOLVED] LINQ for converting IList<Role> to string array of Role.Name

    Quote Originally Posted by AceInfinity View Post
    Enjoy, hope it works for what you're trying to do here
    Custom Membership and Role Providers using nHibernate.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width