Fastest way to retrieve values from dictionary - c#

I have a var myDictionary = new Dictionary<int, string> with the following data:
123, "Do this"
234, "Do that"
345, "Do something"
123, "Do that"
567, "Do anything"
234, "Do something"
What's the best way for me to retrieve only the values for any given key? Say, I want to get only the values for 123.

If you want to have multiple different values grouped under one key you probably have to change the structure of your dictionary to something like this:
var myDictionary = new Dictionary<int, List<string>>();
You then initialize the list for each new key or if the key's already there you add the item to the list.
if (!myDictionary.ContainsKey(myKey))
{
myDictionary[myKey] = new List<string();
}
myDictionary[myKey].Add(myItem);
And you get the items in a standard way:
if (myDictionary.ContainsKey(myKey))
{
var results = myDictionary[myKey];
}
This will give you a list that you can then query to see what items have been returned.

A Dictionary object cannot have multiple items with the same key. Instead, you want to use KeyValuePair.
The code might look something like this:
var items = new List<KeyValuePair<int, String>>();
items.Add(new KeyValuePair<int, String>(123, "Do this"));
items.Add(new KeyValuePair<int, String>(234, "Do that"));
items.Add(new KeyValuePair<int, String>(345, "Do something"));
items.Add(new KeyValuePair<int, String>(123, "Do that"));
items.Add(new KeyValuePair<int, String>(567, "Do anything"));
items.Add(new KeyValuePair<int, String>(234, "Do something"));
// This is the key you are looking for
int desiredValue = 123;
foreach (var v in items.Where(kvp => kvp.Key == desiredValue))
{
// Access the values you need here
Console.WriteLine(v.Value);
}
Where the output would be:
Do this
Do that
You can see this example in action here. Happy coding :)

See code below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication61
{
class Program
{
static void Main(string[] args)
{
Dictionary<int, List<string>> dict = new Dictionary<int, List<string>>() {
{123, new List<string>() {"Do this", "Do that"}},
{234, new List<string>() {"Do that", "Do something"}},
{345, new List<string>() {"Do something"}},
{567, new List<string>() {"Do anything"}}
};
List<string> results = dict[123];
}
}
}

Related

Adding Data in List inside a Dictionary in C#

In Step 1 I wrote this code to access pre-existing list & add value in it .
In Step 2 I updated the dictionary with new list.
In Step 3 again I have to access the list inside dictionary to print the result.
Is there any process or shortcut to add new values to this pre-existing list directly inside dictionary without updating it?
Only have to write the code inside Main. Rest was hardcoded in the compiler & can't be changed.
Your Help will be appreciated. Suggestions are welcome :)
using System;
using System.Collections.Generic;
namespace AddNewMember
{
public class Club
{
static Dictionary<int, string> groupInfo = new Dictionary<int, string>() { { 1, "Gold" }, { 2, "Silver" }, { 3, "Platinum" } };
static Dictionary<int, List<String>> memberInfo = new Dictionary<int, List<String>>() {
{ 1, new List<string>(){ "Tom","Harry"} },
{ 2,new List<string>(){ "Sam","Peter"} },
{ 3,new List<string>(){ "Kim","Robert"} } };
public static void Main(string[] args)
{
//Write your code here. Above part is hardcoded can't be changed
Console.WriteLine("Group Name :");
string gName = Console.ReadLine();
int num = 0;
foreach (KeyValuePair<int, string> VARIABLE in groupInfo)
{
if (VARIABLE.Value == gName)
{
num = VARIABLE.Key;
}
}
Console.WriteLine("Member Name:");
string name = Console.ReadLine();
//Step 1
List<string> l = memberInfo[num];
l.Add(name);
//Step 2
memberInfo[num] = l;
//Step 3
List<string> r = memberInfo[num];
foreach (var VARIABLE in r)
{
Console.WriteLine(VARIABLE);
}
}
}
}
Seems to me like your understanding of dictionaries is upside down. You use the key to retrieve the value, not the other way round. If you're looking to have the user enter a group name (gold, silver, bronze) and then the name of the person to add to that group, you should make the dictionary map a string (group name) to a list of members
static Dictionary<string, List<String>> groupInfo = new() {
{ "Gold", new(){ "Tom","Harry" } },
{ "Silver", new(){ "Sam","Peter"} },
{ "Platinum", new(){ "Kim","Robert"} }
};
public static void Main(string[] args)
{
Console.WriteLine("Group Name :");
string gName = Console.ReadLine();
Console.WriteLine("Member Name :");
string mName = Console.ReadLine();
groupInfo[gName].Add(mName);
}
Yep, that's it. GroupInfo maps string group names to list of string member names, calling groupInfo[gName] resolves to a List of string, so the Add method call there is being performed on the list and the given member name is added
Side note, I'm leveraging a facility of recent c# where you don't have to repeat the type name on both sides of the =. The compiler will know that groupInfo is a Dictionary<string, List<string>> and when it seems new() in the initializer it knows I mean new List<string> which can really help tidy things up. The parentheses are necessary otherwise it would think I was trying to make an anonymous type, which is a different thing. If you get compiler errors you might have to restore the type names if your c# is older
We don't need to re-assign modified list into dictionary value. Step #2 is redundant. When you retrieve the list from step #1. It return a pointer (reference) to the list in the dictionary. It means, when you perform insert an item into the list variable, the list in dictionary is updated (new item added).
Also, In step #3, you get the r but not used.

C# Looping through Dictionary and summing values for Keys

I have a Dictionary<string, decimal?>, and i would like to be able to sum the decimals by distinct string. So say i have the below inputs in dictionary,
"1", 20.00
"1", 35.00
"2", 10.00
"3", 15.00
"3", 30.00
I would like the following output to a new Dictionary
"1", 55.00
"2", 10.00
"3", 45.00
I'm guessing it would be something like
foreach(var item in dictionary)
{
newDictionary.Add(not sure what would go here, maybe some sort of linq query for distinct and sum?);
}
Assuming the same List of key value pairs as in the other answers:
var myList = New List<KeyValuePair<string, decimal?>> {
new KeyValuePair<string, decimal?>("1", (decimal)10.00),
new KeyValuePair<string, decimal?>("1", (decimal)15.00),
new KeyValuePair<string, decimal?>("2", (decimal)20.00),
new KeyValuePair<string, decimal?>("3", (decimal)30.50),
new KeyValuePair<string, decimal?>("3", (decimal)17.500)
};
var myResults = myList.GroupBy(p => p.Key)
.ToDictionary(g => g.Key, g=>g.Sum(p=>p.Value))
The keys in a dictionary can't be repeated, so the 2 first entries won't fit in a dictionary.
I think you may have a list of objects that you can loop, then you can use a dictionary to store the total for each "key"
something like
Dictionary<string, double> totals = new Dictionary<string, double>();
List<Tuple<string, double>> entries = new List<Tuple<string, double>>() {
new Tuple<string, double>("1",20.00),
new Tuple<string, double>("1",35.00),
new Tuple<string, double>("2",10.00),
new Tuple<string, double>("3",15.00),
new Tuple<string, double>("3",30.00)
};
foreach (var e in entries)
{
if (!totals.Keys.Contains(e.Item1))
{
totals[e.Item1] = 0;
}
totals[e.Item1] += e.Item2;
}
You can not have Dictionary object with duplicate keys. You would see ArgumentException when you try to add an existing key into the Dictionary object.
Refer: https://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx
As mentioned here You can't use Dictionary if you are using same keys, because they must be Unique.
You can use list of KeyPair though, which is closest to Dictionary, then you're code will look like this:
List<KeyValuePair<string, decimal?>> myList = new List<KeyValuePair<string, decimal?>>();
myList.Add(new KeyValuePair<string, decimal?>("1", (decimal)10.00));
myList.Add(new KeyValuePair<string, decimal?>("1", (decimal)15.00));
myList.Add(new KeyValuePair<string, decimal?>("3", (decimal)30.50));
myList.Add(new KeyValuePair<string, decimal?>("3", (decimal)17.500));
Dictionary<string, decimal?> sums = new Dictionary<string, decimal?>(); //This can be a dictionary because the keys will be unique after grouping
foreach (var item in myList.GroupBy(m => m.Key))
{
string currentKey = item.FirstOrDefault().Key;
sums.Add(currentKey, myList.Where(j => j.Key == currentKey).Sum(o => o.Value));
}

Manipulating data in C# Set operations

I have two lists of dictionary on which I am doing set operations. I will illustrate what I am doing and what I am trying to achieve as an example:
var A = new List<Dictionary<string, int>>();
var B = new List<Dictionary<string, int>>();
A.Add(new Dictionary <string, int> { {"KeyA",1}, {"KeyB", 2} });
A.Add(new Dictionary <string, int> { {"KeyA",3}, {"KeyB", 5} });
B.Add(new Dictionary <string, int> { {"KeyA",6}, {"KeyB", 8}, {"KeyC", 11} });
B.Add(new Dictionary <string, int> { {"KeyA",3}, {"KeyB", 7}, {"KeyC", 15} });
var result = A.Intersect(B, new KeyComparer("KeyA"));
This gives me back the list of A using KeyA for intersection and I get a single row with {"KeyA",3}, {"KeyB", 5} as the result. What if I want to add {KeyC, 15} from B to the result when intersection has happened.
I hope I am clear with the question.
If I've correctly understood your question, you want to add some other item to the sequence once you've done the intersection... and then, what you need is Enumerable.Concat:
var result = A.Intersect(B, new KeyComparer("KeyA"))
.Concat(B.Last().Where(pair => pair.Key == "KeyC"));

Get the value from list

I create the list like
var list = new List<KeyValuePair<string, string>>();
list.Add(new KeyValuePair<string, string>("1", "abc"));
list.Add(new KeyValuePair<string, string>("2", "def"));
list.Add(new KeyValuePair<string, string>("3", "ghi"));
How to select the value from this list.
Which means I need to pass 1 to the list and need to take the equal value "abc".How to do this? input is 1,output is abc.
It sounds like you just want:
var value = list.First(x => x.Key == input).Value;
That's if you're sure the key will be present. It's slightly trickier otherwise, partly because KeyValuePair is a struct. You'd probably want:
var pair = list.FirstOrDefault(x => x.Key == input);
if (pair.Key != null)
{
// Yes, we found it - use pair.Value
}
Any reason you're not just using a Dictionary<string, string> though? That's the more natural representation of a key/value pair collection:
var dictionary = new Dictionary<string, string>
{
{ "1", "abc" },
{ "2", "def" },
{ "3", "ghi" }
};
Then:
var value = dictionary[input];
Again, assuming you know the key will be present. Otherwise:
string value;
if (dictionary.TryGetValue(input, out value))
{
// Key was present, the value is now stored in the value variable
}
else
{
// Key was not present
}
Why are you not using a Dictionary? http://msdn.microsoft.com/en-us/library/xfhwa508.aspx
It seems to me this would solve your problem, and it's much easier to use.

Creating a random name generator. How do I accomplish this?

I'm trying to grab a single item from each of the Lists here, and combine them to make a unique name. This is just for kicks. :)
Here are the lists:
List<string> FirstNames = new List<string>()
{
"Sergio",
"Daniel",
"Carolina",
"David",
"Reina",
"Saul",
"Bernard",
"Danny",
"Dimas",
"Yuri",
"Ivan",
"Laura"
};
List<string> LastNamesA = new List<string>()
{
"Tapia",
"Gutierrez",
"Rueda",
"Galviz",
"Yuli",
"Rivera",
"Mamami",
"Saucedo",
"Dominguez",
"Escobar",
"Martin",
"Crespo"
};
List<string> LastNamesB = new List<string>()
{
"Johnson",
"Williams",
"Jones",
"Brown",
"David",
"Miller",
"Wilson",
"Anderson",
"Thomas",
"Jackson",
"White",
"Robinson"
};
I know I get a single item via an index, and I also know that I can use the Random class to generate a random number from 0 to ListFoo.Count.
What I don't know is how to check if a random permutation has already been drawn from the collections.
I've thought about using the tuple class:
List<Tuple<int,int,int>> permutations = new List<Tuple<int,int,int>>();
But I'm having a brainfart here. ;) Any guidance? I'm not really looking for the entire code to this simple problem, just a suggestion or hint.
EDIT
Thanks to the suggestions given here, here what I've come up with. Any room for improvements?
static void Main(string[] args)
{
List<string> FirstNames = new List<string>()
{
"Sergio",
"Daniel",
"Carolina",
"David",
"Reina",
"Saul",
"Bernard",
"Danny",
"Dimas",
"Yuri",
"Ivan",
"Laura"
};
List<string> LastNamesA = new List<string>()
{
"Tapia",
"Gutierrez",
"Rueda",
"Galviz",
"Yuli",
"Rivera",
"Mamami",
"Saucedo",
"Dominguez",
"Escobar",
"Martin",
"Crespo"
};
List<string> LastNamesB = new List<string>()
{
"Johnson",
"Williams",
"Jones",
"Brown",
"David",
"Miller",
"Wilson",
"Anderson",
"Thomas",
"Jackson",
"White",
"Robinson"
};
var permutations = new List<Tuple<int, int, int>>();
List<string> generatedNames = new List<string>();
Random random = new Random();
int a, b, c;
//We want to generate 500 names.
while (permutations.Count < 500)
{
a = random.Next(0, FirstNames.Count);
b = random.Next(0, FirstNames.Count);
c = random.Next(0, FirstNames.Count);
Tuple<int, int, int> tuple = new Tuple<int, int, int>(a, b, c);
if (!permutations.Contains(tuple))
{
permutations.Add(tuple);
}
}
foreach (var tuple in permutations)
{
generatedNames.Add(string.Format("{0} {1} {2}", FirstNames[tuple.Item1],
LastNamesA[tuple.Item2],
LastNamesB[tuple.Item3])
);
}
foreach (var n in generatedNames)
{
Console.WriteLine(n);
}
Console.ReadKey();
}
You are on the right track!
Every time you generate a name, add it to your tuple list
//Create the tuple
Tuple <int, int, int> tuple = new Tuple<int, int, int>(index1, index2, index3)
if(!permutations.Contains(tuple))
{
permutations.Add(tuple);
//Do something else
}
I would think the simplest solution is to just the stuff the assembled name into a HashSet<string> which will ensure the list of created names is unique.
An alternative to the HashSet answer is to build all of the possible combinations in advance, shuffle them, then store them in a Queue, where you can retrieve them one at a time. This will avoid having to check the existing ones every time you build a new one, and will still be random.
This only works if you don't have a large set to begin with, since the work involved in creating the complete list and shuffling it would be huge for a large set of data.
It's really easy to generate them all using LINQ:
var combs =
(from first in FirstNames
from second in LastNamesA
from third in LastNamesB
select new Tuple<string, string, string>(first, second, third)).ToList();
After this, if you need to take unique elements from the list randomly, just shuffle the list and then pick them one-by-one in order.
You can use the Knuth-Fisher-Yates algorithm (that's an in-place shuffle):
Random rand = new Random();
for (int i = combs.Count - 1; i > 0; i--)
{
int n = rand.Next(i + 1);
var mem = combs[i];
combs[i] = combs[n];
combs[n] = mem;
}
I would create a HashSet<int> and store the numeric representation of the picks (eg 135 for first, third and 5th or use 010305) and then check if they are in the set.
Create a new tuple with 3 random digits
Check if permutations contains your new tuple
If not => Add new tuple to the list. If yes, start with point 1 again.

Categories

Resources