let dTuples = new Dictionary<int, string * string * int>()
dTuples.Add(1, ("A", "OK", 1))
dTuples.Add(2, ("B", "NOK", 2))
dTuples.Add(3, ("C", "OK", 3))
I want to find the items from the dictionary where they have “OK” in the second element of the value part.
I also want to convert the result to an array of tuple without the key part of the dictionary.
For the above example, I need this:
let tuplesOK = [| ("A", "OK", 1); ("C", "OK", 3) |]
If you provide the code, please also add some explanation, so I can understand it better!
Thanks,
John
The solution by Daniel clearly answers your question, but if you need to perform a lookup in the dictionary based on something else than the key, then you may want to consider using a different data structure, because there is no point in using the dictionary. Maybe you could just use a list:
And then perform lookup using
Seq.filterjust like in the solution by Daniel:Or if you need you’re going to perform the lookup often, you could group the values using the second element (OK/NOK flag) and then store that in a dictionary using the
dictfunction: