I have a dictionary with an array in it defined as:
Dictionary<string, string[]> wordDictionary = new Dictionary<string, string[]>();
is there a way in c# to access specific values in the dictionary without the foreach iteration.
Try this:
var t = wordDictionary ["myKey"][myArrIndex]
For example, this will give you the whole array:
var t = wordDictionary ["myKey"]
while this will give you the value in the array at position 5:
var t = wordDictionary ["myKey"][5]
If you know the key you can access it like this:
string[] str=wordDictionary["yourString"];
What about this?
string firstFoo = wordDictionary["foo"][0]
Related
I have a Dictionary<> that's set up as follows:
Dictionary<int, List<string>> srt = new Dictionary<int, List<string>>();
and I would like to access a specific string in the List<string> (which is the TValue in the Dictionary<>
).
For example -> Given: Dictionary<1, {"string1", "string2", "string3"}>, how can I access "string2", and only "string2" specifically.
I hope the question was clear enough to understand. If it wasn't, tell me what I need to clarify.
Thanks!
Is this what you want?
Dictionary<int, List<string>> dict = new Dictionary<int, List<string>>();
dict .Add(1, new List<string> { "Hello", "World" });
string result = fileList[1][1]; //Will output "World"
try this:
var res = srt[1][0]; //"string1"
First index is a key and second index refers to list element
You can use this
var concreteValue = str.Values.SelectMany(l => l.Where(v => v.Equals("string2")))
.FirstOrDefault();
It's pretty simple actually. Access by key to the specific list then you can treat it as an array. The syntax would be srt[key][index]
Which means to access string2 specifically you need write as var b = srt[1][1]; here b will have string2
I have a list like,
List<string> list = new List<string>();
list.Add("MEASUREMENT");
list.Add("TEST");
I have a dictionary like,
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("BPGA", "TEST");
dict.Add("PPPP", "TEST");
dict.Add("RM_1000", "MEASUREMENT");
dict.Add("RM_2000", "MEASUREMENT");
dict.Add("CDMA", "TEST");
dict.Add("X100", "XXX");
Now, I want to get all matched data from dictionary based on list.
Means, all data from list match with dict value then get new dictionary with following mathched values
Is there any way to achieve this by using lambda expression?
I want result like this.
Key Value
"BPGA", "TEST"
"PPPP", "TEST"
"RM_1000", "MEASUREMENT"
"RM_2000", "MEASUREMENT"
"CDMA", "TEST"
Thanks in advance!
You should be using the dictionary like it is intended to be used i.e. a common key with multiple values for example:
Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
Then all you need to do when adding the values is:
dict.Add("TEST", new List<string>() { /*strings go in here*/ });
Then to get all the results from a key like:
List<string> testValues = dict["TEST"];
To make it safe however you should check that the key exists i.e.
if (dict.ContainsKey("TEST"))
{
//Get the values
}
Then to add values to a current key you go do something like:
dict["TEST"].Add("NewValue");
If you insist on keeping the same structure, although I do not recommend it, something like the following will work:
List<string> testKeys = new List<string>();
foreach (var pairs in dict)
{
if (pair.Value == "TEST")
{
testKeys.Add(pair.Key);
}
}
Or even the following LINQ statement:
List<string> testKeys = dict.Where(p => p.Value == "TEST").Select(p => p.Key).ToList();
For a generic query to find the ones from your list use:
List<string> values = dict.Where(p => list.Contains(p.Value)).ToList();
I want to create a list of strings whose name should be the value of string variable.
Overall I want to assign a variable value to a (object) listname.
(Pseudo Code):
string s = "listname";
list<String>.Name = s;
Closest thing you can do is use a Dictionary to reference the variables.
Dictionary<string, List<string>> myLists = new Dictionary<string, List<string>>();
var s = "listname";
myLists.Add(s, new List<string>());
// To access
var list = myLists[s];
I've a class like as below:
public class Source
{
...
...
public List<Dictionary<int, int>> blanks { get; set; }
}
I've created an object of this and a Dictionary for it. I filled 'dic' Dictionary. Then, I add this dic to the blanks list.
Source src = new Source();
Dictionary<int, int> dic = new Dictionary<int, int>();
dic.Add(30, 50);
dic.Add(40, 60);
src.blanks.Add(dic);
And I try to access these 'Key' and 'Value' elements. But, I can't.
int a = src.blanks[0].Key;
int b = src.blanks[0].Value;
What can I do to access these elements?
Thanks.
src.blanks[0] is a whole dictionary, not a single KeyValuePair<int,int>. That is why you cannot access a .Key or .Value on it - there are potentially many keys, and many values associated with them.
You can access all key-value pairs in a dictionary at position zero by enumerating them, like this:
foreach (var kvp in src.blanks[0]) {
int a = kvp.Key;
int b = kvp.Value;
Console.WriteLine("Key:{0} Value:{1}", a, b);
}
blanks[0] returns a Dictionary<int, int>, you need to specify key of your item.
src.blanks[0][key]
Or loop through your values:
foreach(var pair in src.blanks[0])
{
int currentKey = pair.Key;
int currentValue = pair.Value;
}
You are trying to access a dictionary in the list which has no Key property. The Keyvaluepairs in a dictionary have keys.
So assuming you want to look into the first dictionary in the list:
Dictionary<int, int> dict = s.blanks[0];
// lookup 30:
int value = dict[30]; // 40
to get the value you should first index the list and then index the dictionary
to get value
int b = (src.blanks[0])[0]
You want something like the following:
var listItem = src.blanks[0];
var dictionaryItem = listItem[0];
var a = dictionaryItem.Key;
var b = dictionaryItem.Value;
Nevertheless, i advice you to get rid of those "nested generics" List<Dictionary<..,..>. You won't be able to distinguish what kind of object you're dealing with if you use these nested structs.
Use other structs that better represent your business logic. For example, you could derive your own class from List
I will just list out some arrays and ask how to do them in C#.
$myArray = array();
In PHP I don't have to declare a memory size for the array. What if I don't know big I need my array?
$myArray = array("Name" => "Steve");
How do I do the above in C#?
$myArray = array();
$myArray['Names'][0] = "Steve";
$myArray['Names'][1] = "Jim";
How does the above work in C#"?
$myArray = array("Name" => "Steve");
This is a map. PHP doesn't need you to know that, but C# does. You would implement this as:
var myArray = new Dictionary<String, String>();
For your second case
$myArray['Names'][0] = "Steve";
This is a dictionary where the keys are Strings, but the values are String[]. In C#:
var myArray = new Dictionary<String, List<String>>();
arrays in PHP are most like maps or C# Dictionary . An array in PHP is actually what is called an associative array . So for your code above the C# equivalent is:
Dictionary<string, string> items= new Dictionary<string, string>();
items.Add("Name", "Steve");
if you want a key to point to multiple values:
Dictionary<string, ICollection<string>> items= new
Dictionary<string, ICollection<String>>();
ICollection<string> names = new List<string>();
names.Add("Steve");
items.Add("Name", names);