Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8953979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:12:44+00:00 2026-06-15T14:12:44+00:00

I have following code that checks whether userRoles collection has any of the values

  • 0

I have following code that checks whether userRoles collection has any of the values in authorizedRolesList. It does not work if the userRoleName has a whitespace.

What is the most efficient LINQ way for handling this?

CODE

        List<string> authorizedRolesList = null;
        string AuthorizedRolesValues = "A, B ,C,D";
        if (!String.IsNullOrEmpty(AuthorizedRolesValues))
        {
            authorizedRolesList = new List<string>((AuthorizedRolesValues).Split(','));
        }

        string userRoleName = String.Empty;

        Collection<string> userRoles = new Collection<string>();
        userRoles.Add("B   ");

        bool isAuthorizedRole = false;
        if (userRoles != null)
        {
            foreach (string roleName in userRoles)
            {
                userRoleName = roleName.Trim();
                if (authorizedRolesList != null)
                {
                    //Contains Check
                    if (authorizedRolesList.Contains(userRoleName))
                    {
                        isAuthorizedRole = true;
                    }
                }

            }
        }

REFERENCE:

  1. When to use .First and when to use .FirstOrDefault with LINQ?
  2. Intersect with a custom IEqualityComparer using Linq
  3. Ignoring hyphen in case insensitive dictionary keys
  4. C#: splitting a string and not returning empty string
  5. When does IEnumerable.Any(Func) return a value?
  6. Is IEnumerable.Any faster than a for loop with a break?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:12:46+00:00Added an answer on June 15, 2026 at 2:12 pm

    I guess most efficient LINQ way means most readable here.

    The obvious way is to use StringSplitOptions.RemoveEmptyEntries when calling Split() and not storing the whitespace in the first place.

    authorizedRolesList = AuthorizedRolesValues.Split(new []{','}, StringSplitOptions.RemoveEmptyEntries);
    

    But if for some reason you want to keep the additional whitespace or can’t change the entries in authorizedRolesList, you can easily change your if clause from

    if (authorizedRolesList.Contains(userRoleName))
    

    to

    if (authorizedRolesList.Any(x => x.Trim() == userRoleName))
    

    BTW, talking about LINQ:

    You could just replace your code with

    bool isAuthorizedRole = userRoles.Any(ur => authorizedRolesList.Any(ar => ar.Trim() == ur.Trim()))
    

    if you ensure userRoles and authorizedRolesList are not null (use an empty collection instead).

    Even more readable IMHO would be something like

    bool isAuthorizedRole = userRoles.Intersect(authorizedRolesList, new IgnoreWhitespaceStringComparer()).Any();
    

    where IgnoreWhitespaceStringComparer would look like

    class IgnoreWhitespaceStringComparer : IEqualityComparer<string>
    {
        public bool Equals(string x, string y)
        {
            return x.Trim().Equals(y.Trim());
        }
    
        public int GetHashCode(string obj)
        {
            return obj.Trim().GetHashCode();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code that does not work due to a being a value
I have following code that does not work: I never get to goToFoodDetail .
I have the following piece of code that checks whether port 443 is open
I have the following code that has @item.ID from razor: <a href=# id=deleteitem(@item.ID)>Delete</a> When
I have the following code on my Arduino that constantly checks for a serial
So in my ClientsController.rb I have a before filter that does the following: Checks
I have following code that I am compiling in a .NET 4.0 project namespace
I have following code snippet that i use to compile class at the run
I have the following code that gets the MAC address of an interface: static
I have the following code that opens a new popup window while disabling the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.