Noah Heldman Noah Heldman - 28 days ago 12
C# Question

Use Linq to filter Hashtable based on a property of Values custom object

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...

Answer

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.