包含和等于(==)之间的区别 [英] Difference between contains and equal(==)

查看:620
本文介绍了包含和等于(==)之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Dictionary属性

I have a Dictionary property

 public Dictionary<string, bool> SearchType { get; set; }

此词典有4个键和该键的4个值。现在,如果值是

this Dictionary has 4 keys and 4 values for that keys. Now I take them to a variable from SearchType, if the values are true

var searchTypes = searchDetail.SearchType.Where(x => x.Value == true).ToList();

在下面的代码中,我检查了密钥为CKBinstituteType或CKBstate等

Here I checked the key is CKBinstituteType or CKBstate and etc, from the below code

foreach (var searchType in searchTypes)
{
    if (searchType.Key.Contains("CKBinstituteType"))
    {

    }
    if (searchType.Key.Contains("CKBstate"))
    {

    }
    if (searchType.Key.Contains("CKBlocation"))
    {

    }
    if (searchType.Key.Contains("CKBdistance"))
    {

    }
 }

或以这种方式尝试过(使用等号运算而不是包含)

or tried with this way (used equal operation instead of contains)

foreach (var searchType in searchTypes)
{
    if (searchType.Key == "CKBinstituteType")
    {

    }
    if (searchType.Key == "CKBstate")
    {

    }
    if (searchType.Key == "CKBlocation")
    {


    }
    if (searchType.Key == "CKBdistance")
    {

    }                   
}

它们之间有什么区别?哪一个对我的情况有利? (例如性能,代码标准等)

What is the difference between of them? Which one is good for my situation? (like performance, code standard,etc)

推荐答案


它们之间有什么区别?

What is the different between of them?

两者,包含等于正在使用字符串比较。由于您的 Key 是字符串类型,因此包含将检查所传递的参数是否是密钥的一部分,而等于比较完整字符串是否相等。

Both, Contains and Equals are using string comparison. Since your Key is of type string, Contains will check if the passed parameter is part of the key, whereas Equals compares the complete string for equality.


哪个人适合我的情况? (例如性能,代码
标准等)

Which one is good for my situation? (like performance, code standard,etc)

使用 ContainsKey 方法,而不是等于或包含字符串。
包含和等于在 foreach 循环中使用,您在其中比较 Key ,这是一个 string ,其中包含等于。您无需重复字典中的每个项目。如果您尝试通过Key访问它,那是关于进行线性搜索,其复杂度为 O(n),而进行字典查找的复杂度为 O (1)

Use ContainsKey method , instead of string equals or contains. Contains and Equals are used inside the foreach loop, where you are comparing the Key which is a string with Contains and Equals. You don't need to iterate each item in the dictionary. If you are trying to access it through Key, It is about doing a Linear search with complexity of O(n) vs doing dictionary lookup with complexity O(1)

您可以使用 ContainsKey Like

You can use ContainsKey Like

if (SearchType.ContainsKey("CKBinstituteType"))
{
}

当前您正在将字典转换为列表,我不确定是否确实需要将字典转换为列表,然后进行线性搜索。如果确实需要根据 true 值过滤出Dictionary,则将结果集投影到字典中,然后使用 ContainsKey 像这样:

Currently you are converting your Dictionary to List, I am not sure if there is really a need your dictionary to a List and then do a linear search. If you really have to filter out the Dictionary based on true values then project the result set into a dictionary and then use ContainsKey like:

var searchTypes = searchDetail.SearchType.Where(r => r.Value == true)
                            .ToDictionary(r => r.Key, r => r.Value);
if (searchTypes.ContainsKey("CKBinstituteType"))
{

}
if (searchTypes.ContainsKey("CKBstate"))
{

}
if (searchTypes.ContainsKey("CKBlocation"))
{


}
if (searchTypes.ContainsKey("CKBdistance"))
{

}

这篇关于包含和等于(==)之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆