Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在这种情况下,如何检查键值对映射?_C#_.net_Linq_C# 4.0_Key Value - Fatal编程技术网

C# 在这种情况下,如何检查键值对映射?

C# 在这种情况下,如何检查键值对映射?,c#,.net,linq,c#-4.0,key-value,C#,.net,Linq,C# 4.0,Key Value,我想定义一个结构来帮助我维护这个键值对列表- "ABC", "010" "ABC", "011", "BAC", "010" "BAC" , "011" "CAB", "020" 然后我想写一个传入的方法(“ABC”,“010”),看看这个映射是否存在&如果存在,方法返回true 我应该使用什么结构,该方法看起来如何 我试过- public bool IsAllowed(string source, string dest) { bool allowe

我想定义一个结构来帮助我维护这个键值对列表-

"ABC", "010"
"ABC", "011",
"BAC", "010"
"BAC" , "011"
"CAB", "020"
然后我想写一个传入的方法(“ABC”,“010”),看看这个映射是否存在&如果存在,方法返回true

我应该使用什么结构,该方法看起来如何

我试过-

 public bool IsAllowed(string source, string dest)
        {
            bool allowed = false;
            var allowedDest = new List<KeyValuePair<string, string>>()
            {
                new KeyValuePair<string, string>("ABC","010"),
                new KeyValuePair<string, string>("ABC","011"),
                new KeyValuePair<string, string>("BAC","010"),                
                new KeyValuePair<string, string>("BAC","011"),
                new KeyValuePair<string, string>("CAB","020"),
                new KeyValuePair<string, string>("CAB","030")
            };

             // How to check for mapping?

            return allowed;
        }
public bool是允许的(字符串源,字符串目标)
{
bool-allowed=false;
var allowedest=新列表()
{
新的KeyValuePair(“ABC”、“010”),
新的KeyValuePair(“ABC”、“011”),
新的KeyValuePair(“BAC”、“010”),
新的KeyValuePair(“BAC”、“011”),
新的KeyValuePair(“CAB”、“020”),
新的KeyValuePair(“CAB”、“030”)
};
//如何检查映射?
允许返回;
}

如果是一个大列表,我会声明

HashSet<Tuple<string,string>> Allowed = new HashSet<Tuple<string,string>>();

Allowed.Add(Tuple.Create<string,string>("ABC","010");
[... and all the others]

if (Allowed.Contains(Tuple.Create<string,string>("ABC","010")) { }
HashSet Allowed=new HashSet();
允许添加(元组创建(“ABC”、“010”);
[…和所有其他人]
if(Allowed.Contains(Tuple.Create(“ABC”,“010”){}

如果它是一个小列表,您可以使用foreach语句或.Any()命令对其进行迭代。

您可以简单地使用字符串数组

public bool IsAllowed(string source, string dest)
{
    var allowedDest = new []
    {
        new [] {"ABC", "010"},
        new [] {"ABC", "011"},
        new [] {"BAC", "010"}
        //...
    };

    var match = new [] { source, dest };

    return allowedDest.Any(x => x.SequenceEqual(match));
}

您需要在这里使用Linq方法

通过比较列表中项目的键和值,可以使用FirstOrDefault方法从具有匹配源和目标的列表中检索项目

如果找到项,将返回该项,否则将返回KeyValuePair的默认值。 然后,您需要检查它是否返回默认值,并在此基础上返回true或false

public bool IsAllowed(string source, string dest)
    {
        bool allowed = false;
        var allowedDest = new List<KeyValuePair<string, string>>()
        {
            new KeyValuePair<string, string>("ABC","010"),
            new KeyValuePair<string, string>("ABC","011"),
            new KeyValuePair<string, string>("BAC","010"),                
            new KeyValuePair<string, string>("BAC","011"),
            new KeyValuePair<string, string>("CAB","020"),
            new KeyValuePair<string, string>("CAB","030")
        };
        var item = allowedDest.FirstOrDefault(kvpair => kvpair.Key == source && kvpair.Value == dest);
        allowed = !item.Equals(default(KeyValuePair<string, string>));

        return allowed;
    }
public bool是允许的(字符串源,字符串目标)
{
bool-allowed=false;
var allowedest=新列表()
{
新的KeyValuePair(“ABC”、“010”),
新的KeyValuePair(“ABC”、“011”),
新的KeyValuePair(“BAC”、“010”),
新的KeyValuePair(“BAC”、“011”),
新的KeyValuePair(“CAB”、“020”),
新的KeyValuePair(“CAB”、“030”)
};
var item=allowedest.FirstOrDefault(kvpair=>kvpair.Key==source&&kvpair.Value==dest);
允许=!item.Equals(默认值(KeyValuePair));
允许返回;
}

这应该可以帮助您解决问题。

如何:
allowed=allowedest.Any(c=>c.Equals(newkeyvaluepair(“ABC”,“010”));
感觉您只是想使用一个
字典
。然后检查
TryGetValue
的给定键是否存在值;如果返回true,则映射存在。