Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

380
Vistas
C# obtener claves y valores de List<KeyValuePair<cadena, cadena>

Dada una lista:

 private List<KeyValuePair<string, string>> KV_List = new List<KeyValuePair<string, string>>(); void initList() { KV_List.Add(new KeyValuePair<string, string>("qwer", "asdf")); KV_List.Add(new KeyValuePair<string, string>("qwer", "ghjk")); KV_List.Add(new KeyValuePair<string, string>("zxcv", "asdf")); KV_List.Add(new KeyValuePair<string, string>("hjkl", "uiop")); }

(NOTA: hay varios valores para la clave "qwer" y varias claves para el valor "asdf").

1) ¿Hay una mejor manera de devolver una lista de todas las claves que simplemente hacer un foreach en la lista KeyValuePair?

2) Del mismo modo, ¿hay una mejor manera de devolver una lista de todos los valores para una clave dada que usar un foreach?

3) Y luego, ¿qué tal devolver una lista de claves para un valor dado?

Gracias...

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

// #1: get all keys (remove Distinct() if you don't want it) List<string> allKeys = (from kvp in KV_List select kvp.Key).Distinct().ToList(); // allKeys = { "qwer", "zxcv", "hjkl" } // #2: get values for a key string key = "qwer"; List<string> values = (from kvp in KV_List where kvp.Key == key select kvp.Value).ToList(); // values = { "asdf", "ghjk" } // #3: get keys for a value string value = "asdf"; List<string> keys = (from kvp in KV_List where kvp.Value == value select kvp.Key).ToList(); // keys = { "qwer", "zxcv" }
over 4 years ago · Santiago Trujillo Denunciar

0

Parece que se beneficiaría de usar algo como:

 Dictionary<string, List<string>> kvlist; kvlist["qwer"] = new List<string>(); kvlist["qwer"].Add("value1"); kvlist["qwer"].Add("value2"); foreach(var value in kvlist["qwer"]) { // do something }

Sería relativamente fácil crear una clase básica de diccionario de valores múltiples utilizando un diccionario y una lista.

Esta publicación de blog habla más sobre el tipo MultiDictionary de Microsoft disponible a través de NuGet.

over 4 years ago · Santiago Trujillo Denunciar

0

Puede usar NameValueCollection desde System.Collection.Specialized espacio de nombres:

 NameValueCollection KV_List = new NameValueCollection(); KV_List.Add("qwer", "asdf"); KV_List.Add("qwer", "ghjk"); KV_List.Add("zxcv", "asdf"); KV_List.Add("hjkl", "uiop");

Ejemplo de uso:

 string singleValue = KV_List["zxcv"]; // returns "asdf" string[] values = KV_List.GetValues("qwer"); // returns "asdf, "ghjk" string[] allKeys = KV_List.AllKeys; string[] allValues = KV_List.AllKeys;

https://msdn.microsoft.com/en-us/library/system.collections.specialized.namevaluecollection%28v=vs.110%29.aspx

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda