I am trying to use hash table in linq to fetch the key whose value is ABC
.
What I have done so far:
Hashtable h=new Hashtable ();
h.Add(1 , "ABC");
h.Add(2 , "AgC");
h.Add(3 , "ABC");
h.Add(4 , "AhC");
Expected output: 1, 3 (keys having value "ABC")
ICollection c= h.Keys;
var posi= from a in c
where h[a]="ABC"
select a;
But the above query is not working and giving compile time error.
The error is:
could not find an implementation of the query pattern for source type 'System.Collections.ICollection'.
What I am doing wrong? I am new to C#. How to use Hashtable in LINQ?
You should not use non-generic HashTable
to start with. Use generic Dictionary<int, string>
instead:
var d = new Dictionary<int, string>();
d.Add(1 , "ABC");
d.Add(2 , "AgC");
d.Add(3 , "ABC");
d.Add(4 , "AhC");
var posi = from a in d
where a.Value == "ABC"
select a.Key;