Shorthand conditional in C# similar to SQL 'in' keyword

后端 未结 8 2229
北恋
北恋 2021-01-04 12:44

In C# is there a shorthand way to write this:

public static bool IsAllowed(int userID)
{
    return (userID == Personnel.JohnDoe || userID == Personnel.JaneD         


        
8条回答
  •  猫巷女王i
    2021-01-04 13:36

    How about something like this:

    public static bool IsAllowed(int userID) {
      List IDs = new List { 1,2,3,4,5 };
      return IDs.Contains(userID);
    }
    

    (You could of course change the static status, initialize the IDs class in some other place, use an IEnumerable<>, etc, based on your needs. The main point is that the closest equivalent to the in operator in SQL is the Collection.Contains() function.)

提交回复
热议问题