acurachris acurachris - 1 year ago 76
C# Question

Check if any part of a hashtable value contains certain string c#

I deserialise a file into a hashtable of which the keys contain file paths like,

for example. I then need to check if part of a path
is included in one of those hashtable keys. So if

I can do this simply with a list below (lstLoadedNodes is the List) which works great:

if (lstLoadedNodes.Any(s => s.Contains(e.Node.Path)))

I can't find a way to do this with a hashtable without the whole key needing to be matched.

When I attempt this with a hashtable (checkedNodes is my hashtable) it only returns
if the exact string matches:

if (checkedNodes.ContainsKey(e.Node.Path))

How can I search any part of the hashtable.key? Hope that makes sense!

I know I can pass all the values to a list first and then perform the "if contains.any" which is what i'm currently doing but this seems pointless so I'd rather just check the hashtable.

Thanks in advance.

Answer Source

I think you are looking for something like:

checkedNodes.Keys.Cast<string>().Any(k => k.Contains(e.Node.Path));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download