I have a synchronized Hashtable with int as the key, and a custom class called Job as the value. I would like to filter this Hashtable based on a property in my Job class called JobSize. JobSize is just an enum with values Small, Medium, and Large.
It's fine if it needs to be converted to another collection type to do this.
I know there's a slick LINQy way to do this, but I haven't found it yet...
It looks like this will work for me:
var smallJobs = hashTable.Values.Cast<Job>().Where(job => job.JobSize == JobSize.Small);
The ".Cast<Job>()" is required because Hashtable is non-generic.
Do you need to maintain the keys and values in your filtered Hashtable? If so, try this.
It'll filter the Hashtable and return the filtered results as a strongly typed Dictionary<int,Job>:
var filtered = yourHashtable.Cast<DictionaryEntry>()
.Where(x => ((Job)x.Value).JobSize == JobSize.Small)
.ToDictionary(x => (int)x.Key, x => (Job)x.Value);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With