2-dimensional array with duplicated index key - c#

i want to translate this line of code from php to asp.net
$subid[$value['parentid']][] = $value['id'];
i'm not familiar with asp.net data structure, i've tried arraylist but can't insert at [1], dictionary do not allow duplicated keys, anyone have ideas?
thanks

I'm not familiar with PHP (anymore) and SO is not a translation service, but you can use
List<Tuple<int, int>>
or
Lookup<int, int>
instead (assuming that your ids are ints).
var list = new List<Tuple<int, int>>();
list.Add(Tuple.Create(1, 1));
list.Add(Tuple.Create(1, 2));
list.Add(Tuple.Create(2, 3));
list.Add(Tuple.Create(2, 4));
list.Add(Tuple.Create(3, 5));
With Enumerable.ToLookup you can create a Lookup.
var lookup = list.ToLookup(t => t.Item1, t => t.Item2);
Find all products with parent-id = 1:
var parentID1 = lookup[1];
foreach (var value in parentID1)
Console.Write(value);

You can use a Dictionary with a little tweak to achieve what you want.
You create a dicionary like this:
Dictionary<string, List<string>> myDictionary = new Dictionary<string, List<string>>();
You add items to it like:
if(myDictionary.ContainsKey("myKey")) myDictionary["myKey"].Add("myItem");
else
{
myDictionary.Add("myKey", new List<string>(){"myItem"});
}
If you ask for a certain key, it will return a reference to the list with all related items.

Related

Get dictionary key if value contains string

I have this Dictionary:
static Dictionary<int, string> Players = new Dictionary<int, string>();
dictionary.Add(1, "Chris [GC]");
dictionary.Add(2, "John");
dictionary.Add(3, "Paul");
dictionary.Add(4, "Daniel [GC]");
I want to get the key of values that contains "[GC]"
Any idea how?
Thanks.
Use LINQ:
var result = Players.Where(p => p.Value.Contains("[GC]")).Select(p => p.Key);
Use a query like below.
var itemsWithGC = dictionary.Where(d => d.Value.Contains("[GC]")).Select(d => d.Key).ToList();
foreach (var i in itemsWithGC)
{
Console.WriteLine(i);
}
Actually there's a more performant way of solving this issue, but it might require some refactor...
Whenever you add a new player containing "[GC]", you can fill a HashSet<int>:
Dictionary<int, string> Players = new Dictionary<int, string>();
HashSet<int> gcPlayers = new HashSet<int>();
dictionary.Add(1, "Chris [GC]");
gcPlayers.Add(1);
dictionary.Add(2, "John");
dictionary.Add(3, "Paul");
dictionary.Add(4, "Daniel [GC]");
gcPlayers.Add(4);
So now getting all keys which have "[GC]" is as easy as using the whole set called gcPlayers.
This will avoid a query and iterate the entire dictionary to get all coincidences, while you'll avoid adding duplicates to gcPlayers because it's a set (i.e. an unordered collection of unique values).

C# sort a dictionary by value

I am sorting a dictionary, consisting of values & keys , by value. I have a hash of words and number of time used, that I want to order by number of time used.
There is a SortedList which is good for a single value , that I want to map it back to the word.
SortedDictionary orders by key, not value.
I could use a custom class, is there a better way.
I did some google searches but I can't find exactly what I am lookign for.
I found the answer
List<KeyValuePair<string, string>> BillsList = aDictionary.ToList();
BillsList.Sort(delegate(KeyValuePair<string, string> firstPair,
KeyValuePair<string, string> nextPair)
{
return firstPair.Value.CompareTo(nextPair.Value);
}
);
This should do it:
Dictionary<string, string> d = new Dictionary<string, string>
{
{"A","Z"},
{"B","Y"},
{"C","X"}
};
d.OrderBy(x=>x.Value).Select(x=>x.Key);
Will return C, B, A.
Here is using Linq and mapping the Count to the Word:
IDictionary<string, int> wordsAndCount = new Dictionary<string, int>
{
{"Batman", 987987987},
{"MeaningOfLife",42},
{"Fun",69},
{"Relaxing",420},
{"This", 2}
};
var result = wordsAndCount.OrderBy(d => d.Value).Select(d => new
{
Word = d.Key,
Count = d.Value
});
Result:

Making a List<string> from Dictionary<string, string>

I was wondering if it were possible to make a list from the dictionary values where the key is a specified value?
The dictionary would like this:
Sidcup - DPC1
Sidcup - DPC2
Blackheath - DPC3
Blackheath - DPC4
Bexleyheath - DPC5
In fact, I'm not entirely implementing a Dictionary as above is a good idea. Here is its implementation:
DataSet ds = EngineBllUtility.GetDPCsForImportFile(connectionString, fileID);
if (ds.Tables.Count > 0)
{
DataTable dtDPCs = EngineBllUtility.GetDPCsForImportFile(connectionString, fileID).Tables[0];
Dictionary<string, string> preliminaryList = new Dictionary<string, string>();
if (dtDPCs.Columns.Contains("DPCNumber") && dtDPCs.Columns.Contains("BranchName"))
foreach (DataRow dataRow in dtDPCs.Rows)
{
preliminaryList.Add(dataRow["BranchName"].ToString(), dataRow["DPCNumber"].ToString());
}
I have the following code: (Excuse the last line, its just so you have an idea of what I'm trying to do).
foreach (string branch in branchNames)
{
string subfolder = System.IO.Path.Combine(saveLocation, branch);
System.IO.Directory.CreateDirectory(subfolder);
List<string> certificateList = new List<string>();
certificateList.Add(DPCNumber in preliminaryList where Key = branch);
}
In the above the branch is the key from the Dictionary. I need to iterate through because it needs to create a new folder and then do something with the certificateList I am creating.
Sure:
private static void TestZip()
{
Dictionary<string, string> stringstringdic = new Dictionary<string, string>();
stringstringdic.Add("1", "One");
stringstringdic.Add("2", "Two");
stringstringdic.Add("3", "Three");
stringstringdic.Add("4", "Four");
stringstringdic = stringstringdic.Where(pair => pair.Key != "1")
.ToDictionary(pair => pair.Key, pair => pair.Value);
List<string> stringlist = stringstringdic.Keys.Concat(stringstringdic.Values).ToList();
foreach (string str in stringlist)
{
Console.WriteLine(str);
}
}
//Output:
//2
//3
//4
//Two
//Three
//Four
Of course, you'll have to change the Where clause to reflect your real need.
If I understood you right, it's like .Where(pair => pair.Key == branch)
If I understand you correctly you want to add the value based on a key to a separate List?
certificateList.Add(preliminaryList[branch])
This is simplified as I really need to see the declaration of preliminaryList to know how DPCNumber fits into all of it. Could it be...
certificateList.Add(preliminaryList[branch].ToString())
To simply create a list of keys you can do the following.
var dictionary = new Dictionary<string, string>();
dictionary.Add("key1", "value1");
dictionary.Add("key2", "value2");
dictionary.Add("key3", "value3");
dictionary.Add("key4", "value4");
dictionary.Add("key5", "value5");
var list = dictionary.Keys.ToList();
This should give you a list with values "key1", "key2", "key3", "key4", "key5".
You can put a where clause in to filter out certain keys. The following gives all keys which contain a "2" (random example), resulting in just "key2".
var filteredList = dictionary.Keys.Where(key => key.Contains("2")).ToList();
Edit:
To get a value given a specific key.
string value = dictionary["key1"];
Note, the key is a dictionary must be unique, so for a given key you will only ever get a single value back and not a list of values.

Create an int[] from Dictionary<int, List<int>>, from each dictionary element's Value List<int> I need an array which contains all dictionary elements

The solution I have for now is as...
Dictionary<int, List<int>> oDict = <Some code to fill in the dictionary>;
var oList = new List<int>();
oDict.Values.ForEach(oList.AddRange);
oList.ToArray();
Is there a way to do this without using the additional List<int>?
Yup, that looks like:
var array = oDict.Values.SelectMany(list => list).ToArray();
(If you only want distinct elements, just call Distinct before ToArray.)

c# sorting a StringDictionary by value, NOT key

What is the 'best' way to sort (or iterate) over a StringDictionary in order of Value (not Key)
E.g. Key - Value
1 - X label
2 - A label
3 - Other label
would give
2 - A label
3 - Other label
1 - X label
EDIT - I meant to say "using .NET 2.0 features". Sorry, me bad...
Use LINQ:
var items = from k in d.Keys
orderby d[k] ascending
select k;
If you are restricted to C# 2.0 features, use this:
IDictionary<string, string> d = new Dictionary<string, string>();
d["1"] = "X label";
d["2"] = "A label";
d["3"] = "Other Label";
List<KeyValuePair<string, string>> myList = new List<KeyValuePair<string, string>>(d);
myList.Sort(
delegate(KeyValuePair<string, string> a,
KeyValuePair<string, string> b)
{
return a.Value.CompareTo(b.Value);
}
);
Note:
If you are using a StringDictionary instead of Dictionary, check out Anthony's solution.
Using the StringDictionary class, here is a method to use LINQ's OrderBy. Assumes you have .NET 3.5.
var sortedDictionary = dictionary.Cast<DictionaryEntry>().OrderBy(pair => pair.Value);
Using 2.0, it's a bit trickier. Here's an approach using a Comparison delegate.
StringDictionary dictionary = new StringDictionary();
dictionary.Add("1", "One");
dictionary.Add("2", "Two");
dictionary.Add("3", "Three");
DictionaryEntry[] sortedDictionary = new DictionaryEntry[dictionary.Count];
dictionary.CopyTo(sortedDictionary, 0);
Comparison<DictionaryEntry> comparison = new Comparison<DictionaryEntry>(delegate (DictionaryEntry obj1, DictionaryEntry obj2) { return ((string)obj1.Value).CompareTo((string)obj2.Value); });
Array.Sort(sortedDictionary, comparison);
So the actual sort would be in the sortedDictionary array.

Categories