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

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-03 07:26:04

问题


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;

回答1:


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();



回答2:


I think you want:

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



回答3:


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);
        }
    }
}



回答4:


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;
    }
}



回答5:


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



回答6:


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




回答7:


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


来源:https://stackoverflow.com/questions/16860643/get-key-from-value-dictionarystring-liststring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!