【问题标题】:Enum array Contains String Dictionary Key C#枚举数组包含字符串字典键 C#
【发布时间】:2021-04-07 09:56:24
【问题描述】:

我正在使用带有 key 、 value 的字符串类型的字典

       var dictionary = new Dictionary<string, string>();
        dictionary.Add("cat",null);
        dictionary.Add("dog", "vals");
        dictionary.Add("Tiger", "s");   

存在这样的自定义枚举类

public enum AnimalName
{      
    Cat = 0,         
    Dog = 1,       
    Rabit = 2,      
    Tiger = 3,        
    Lion = 4
}
 

此枚举中有一个可用的值列表

        AnimalName[] permitted_list   = {
                AnimalName.Cat,
                AnimalName.Dog,
                AnimalName.Rabit
            };
        

我们如何检查字典是否包含数组 allowed_list 中的至少一个键,且值不为空

我使用此代码检查字典中至少存在一个非空值

        //CHECK  atleast one not null value exists                     
        bool canProceed=dictionary.Any(e => !string.IsNullOrWhiteSpace(e.Value));                

我们可以扩展它以检查枚举列表中字典键是否存在

       //Example in this case key Dog exists with a not null value So canProceed is TRUE                               
        //in this case#2 canProceed is FALSE , since the only permitted_list key is Cat and its value is NULL  
        var dictionaryf = new Dictionary<string, string>();
        dictionaryf.Add("cat",null);            
        dictionaryf.Add("Tiger", "s"); 
        dictionaryf.Add("Lion", "exit sjs"); 

【问题讨论】:

  • 当字典针对您所在位置的关键字搜索进行了优化时,我将遍历允许的列表,例如:permitted_list .Select(x =&gt; $"{x}".ToLower()) .Any(x =&gt; dictionary.TryGetValue(x, out string value) &amp;&amp; value != null);

标签: c# dictionary enums


【解决方案1】:

不要使用 new Dictionary(),而是尝试使用 new Dictionary()

它将简化流程并确保您没有错误或拼写错误

【讨论】:

  • 字典键可以是一个巨大的值列表,也可能存在一些非枚举值。这是通过外部 api 从程序的不同来源生成的。所以程序想要从字典中过滤出一组特定键的项目,这些键是通过枚举定义的
【解决方案2】:
bool canProceed=dictionary.Any(e => !string.IsNullOrWhiteSpace(e.Value) && permitted_list.Any(p => p.ToString().ToLower() == e.Key.ToLower()));

【讨论】:

    【解决方案3】:

    你也可以这样做:

            AnimalName animalName = new AnimalName();
            bool canProceed = dictionary.Any(dic => {
                if (Enum.TryParse(dic.Key, true, out animalName) == true)
                {
                    return permitted_list.Any(permittedAnimal => permittedAnimal ==
                animalName);
                }
                else return false;
            });
    

    在这段代码中,首先,我们试图将字典的键解析为枚举。如果可以成功解析,则说明密钥字符串中有一个枚举。下一步将检查是否允许该枚举。如果允许,则 if 条件将返回 true。

    ** 更新:TryParse 方法中的 True 用于忽略大小写。所以,它不会区分大小写。

    【讨论】:

      【解决方案4】:

      我为你准备了这个小麻烦:

      bool canProceed = dictionary.Any(
          e =>
              Enum.TryParse<AnimalName>(e.Key, ignoreCase: true, out var animalName)
              && permitted_list.Contains(animalName)
              && !string.IsNullOrWhiteSpace(e.Value)
      );
      

      让我们向后工作。这是检查该值是否不是您已经拥有的 null 或空格:

      !string.IsNullOrWhiteSpace(e.Value)
      

      这里我们检查permitted_list是否包含animalName

      permitted_list.Contains(animalName)
      

      但是,animalName 必须是 AnimalName 类型,要获得它,我们将使用 Enum.TryParse

      Enum.TryParse<AnimalName>(e.Key, out var animalName)
      

      但是,这行不通,因为您有“猫”,但我们需要“猫”。所以让我们使用重载(我第一次写这篇文章时不知何故错过了):

      Enum.TryParse<AnimalName>(e.Key, ignoreCase: true, out var animalName)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-02
        • 1970-01-01
        • 2011-03-27
        相关资源
        最近更新 更多