Home > database >  find some value is present in Dictionary Object
find some value is present in Dictionary Object

Time:06-04

I have a dictionary<string, string> as follows.

 key = lookupid::lookupschema::lookupcolumn
 value = lookupvalue


        Dictionary<string, string> KeyValueLookup1 = new Dictionary<string, string>();
        KeyValueLookup1.Add("1::ldap::state","TX");
        KeyValueLookup1.Add("2::ldap::state", "MN");
        KeyValueLookup1.Add("3::ldap::state", "CF");
        KeyValueLookup1.Add("4::ldap::language", "EN");
        KeyValueLookup1.Add("5::ldap::language", "FR");

Now I get the following data from user. sample1:

 schemaname = ldap
 lookupcolumn = state
 and value = MN.

Now I need to find out if MN is available in KeyValueLookup1.

 sample2:

 schemaname = ldap
 lookupcolumn = language
 and value = TA.

Now I need to find out if TA is available in KeyValueLookup1.

Also I have to make sure the value that is being looked has also got these key parameters someid::schemaname::lookupcolumn

CodePudding user response:

try this

    var schemaname = "ldap";
    var lookupcolumn = "language";
    var value = "TA";

    var ifAvailable = IfAvailable(KeyValueLookup1, schemaname, lookupcolumn, value); // false

    schemaname = "ldap";
    lookupcolumn = "state";
    value = "MN";

    ifAvailable = IfAvailable(KeyValueLookup1, schemaname, lookupcolumn, value); // true

public bool IfAvailable(Dictionary<string, string> KeyValueLookup, string schemaname, string lookupcolumn, string value)
{
    var result = false;
    foreach (KeyValuePair<string, string> kvp in KeyValueLookup)
    {
        if (kvp.Key.Contains("::"   schemaname   "::"   lookupcolumn) && kvp.Value.Contains(value))
        {
            result = true;
            break;
        }
    }
    return result;
}
  • Related