KeyValuePair List problem?
-
Dear Friends, I have a KeyValuePair List; which i am sorting according to the value added against the respective keys. Now after sorting the list acc. to values i need to fetch the similar key values into a string array (strL4[]). For this I have made a string type array and then i am iterating through each of the record of the KeyValuePair List and storing the key values in the string array. But during addition i need that the iteration must be performed once for all similar types of key values in List so that the string array contains only one distinct key value for multiple similar values.
lstImport.Sort(delegate(KeyValuePairfirst,
KeyValuePairsecond)
{
return first.Value.CompareTo(second.Value);
});
int i=lstImport.Count;
string[] strL4= new string[i];
foreach(KeyValuePair lstI in lstImport)
{
strL4[lstImport.Count - i] = lstI.Key;
i--;
}Can anyone help me out? Thanks Varun Sareen
-
Dear Friends, I have a KeyValuePair List; which i am sorting according to the value added against the respective keys. Now after sorting the list acc. to values i need to fetch the similar key values into a string array (strL4[]). For this I have made a string type array and then i am iterating through each of the record of the KeyValuePair List and storing the key values in the string array. But during addition i need that the iteration must be performed once for all similar types of key values in List so that the string array contains only one distinct key value for multiple similar values.
lstImport.Sort(delegate(KeyValuePairfirst,
KeyValuePairsecond)
{
return first.Value.CompareTo(second.Value);
});
int i=lstImport.Count;
string[] strL4= new string[i];
foreach(KeyValuePair lstI in lstImport)
{
strL4[lstImport.Count - i] = lstI.Key;
i--;
}Can anyone help me out? Thanks Varun Sareen
If what you are saying is that say your KVP list contains:
K1, AAA
K2, BBB
K3, BBB
K4, CCCThen your strings list should only contain 3 elements: "AAA", "BBB", and "CCC", then you need to: 1) Change your array of strings to a List<string> as you don't know how many elements it will contain - you can convert it to an array later if you need to with the ToArray method. 2) Set a string lastValue and set it to the first value, plus the string "Do not match" - this ensures that the first KVP value is transferred to the new list. 3) In your loop, compare the current KVP value to lastValue and if different, use the Add method to add it to your List of strings. 4) At the bottom of your loop, set lastValue to the KVP Value.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
Dear Friends, I have a KeyValuePair List; which i am sorting according to the value added against the respective keys. Now after sorting the list acc. to values i need to fetch the similar key values into a string array (strL4[]). For this I have made a string type array and then i am iterating through each of the record of the KeyValuePair List and storing the key values in the string array. But during addition i need that the iteration must be performed once for all similar types of key values in List so that the string array contains only one distinct key value for multiple similar values.
lstImport.Sort(delegate(KeyValuePairfirst,
KeyValuePairsecond)
{
return first.Value.CompareTo(second.Value);
});
int i=lstImport.Count;
string[] strL4= new string[i];
foreach(KeyValuePair lstI in lstImport)
{
strL4[lstImport.Count - i] = lstI.Key;
i--;
}Can anyone help me out? Thanks Varun Sareen
I think this pattern is what you are looking for:
using System;
using System.Collections.Generic;namespace KeyValuePair
{
class Program
{
static void Main(string[] args)
{
List<KeyValuePair<string, string>> listImport = new List<KeyValuePair<string, string>>();
listImport.Add(new KeyValuePair<string, string>("K1", "CCC"));
listImport.Add(new KeyValuePair<string, string>("K2", "BBB"));
listImport.Add(new KeyValuePair<string, string>("K3", "AAA"));
listImport.Add(new KeyValuePair<string, string>("K4", "BBB"));
listImport.Sort(delegate(KeyValuePair<string, string> first, KeyValuePair<string, string> second)
{
return first.Value.CompareTo(second.Value);
});// We only need to "remember" the last value because the KeyValuePair list is sorted. string strLast = String.Empty; List<string> listDistinct = new List<string>(); foreach (KeyValuePair<string, string> kvp in listImport) { if (strLast == kvp.Value) continue; listDistinct.Add(kvp.Value); strLast = kvp.Value; } foreach (string str in listDistinct) Console.WriteLine(str); Console.ReadKey(); } }
}