简体   繁体   中英

Get key from value - Dictionary<string, List<string>>

I am having trouble getting the key by specifying a value. What is the best way I can achieve this?

var st1= new List<string> { "NY", "CT", "ME" };
var st2= new List<string> { "KY", "TN", "SC" };
var st3= new List<string> { "TX", "OK", "MO" };
var statesToEmailDictionary = new Dictionary<string, List<string>>();
statesToEmailDictionary.Add("test1@gmail.com", st1);
statesToEmailDictionary.Add("test2@gmail.com", st2);
statesToEmailDictionary.Add("test3@gmail.com", st3);

var emailAdd = statesToEmailDictionary.FirstOrDefault(x => x.Value.Where(y => y.Contains(state))).Key;

The return value from FirstOrDefault will be a KeyValuePair<string, List<string>> , so to get the key, simply use the Key property. Like this:

var emailAdd = statesToEmailDictionary
    .FirstOrDefault(x => x.Value.Contains(state))
    .Key;

Alternatively, here's the equivalent in query syntax:

var emailAdd = 
    (from p in statesToEmailDictionary
     where p.Value.Contains(state)
     select p.Key)
    .FirstOrDefault();

我想你想要:

var emailAdd = statesToEmailDictionary.FirstOrDefault(x => x.Value.Any(y => y.Contains(state))).Key;
var emailAdd = statesToEmailDictionary
    .FirstOrDefault(x => x.Value != null && x.Value.Contains(state))
    .Key;

But if you're looking for performance, I'd suggest reversing your dictionary and creating a dictionary of <state, email> to do what you're looking for.

// To handle when it's not in the results
string emailAdd2 = null;
foreach (var kvp in statesToEmailDictionary)
{
    if (kvp.Value != null && kvp.Value.Contains(state))
    {
        emailAdd2 = kvp.Key;
        break;
    }
}

What everyone in this thread failed to mention is that the FirstOrDefault method is only available through Linq :

using System;
using System.Collections.Generic;
// FirstOrDefault is part of the Linq API
using System.Linq;

namespace Foo {
    class Program {
        static void main (string [] args) {
            var d = new Dictionary<string, string> () {
                { "one", "first" },
                { "two", "second" },
                { "three", "third" }
            };
            Console.WriteLine (d.FirstOrDefault (x => x.Value == "second").Key);
        }
    }
}

Simple Linq to do just that

Dim mKP = (From mType As KeyValuePair(Of <Key type>, <Value type>) In <Dictionary>
           Where mType.Value = <value seeked> Select mType).ToList

If mKP.Count > 0 then
    Dim value as <value type> = mKP.First.Value
    Dim key as <Key type> = mKP.First.Key 
End if 

Of course if there are duplicate values this will return more than one KeyValuePair

var emailAdd = statesToEmailDictionary.First(x=>x.Value.Contains(state)).Key;
var temp = statesToEmailDictionary.Where( x => x.Value.Contains(state)).FirstOrDefault();
var emailAdd = temp != null ? temp.Key : string.Empty;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM