Home > front end >  Querying Hashtable key LINQ expression C#
Querying Hashtable key LINQ expression C#

Time:02-10

I have a hashtable that has key/value pairs as follows,

Hashtable h1 = new Hashtable();
h1.Add("@USD_Type","123");

        public class USDParameters
        {
            public const string USDType = "@USD_Type";
            public const string USDId = "@USD_ID";
            public const string USDName = "@USD_Name";
        }

Sometimes, I might also have "@USD_ID", "@USD_Name" keys in the hashtable.

I would like to query all the keys in the hashtable which start with @STD_ and match against the names in USDParameters. If the name is not found in hashtable then I need to build a string that has the key(minus @) from the hashtable.

For e.g. "The following key(s) missing from the hashtable - USD_ID, USD_Name"

CodePudding user response:

The challenging part is to query on the keys:

var keys = h1.Keys.Cast<string>()
  .Where(x => x.StartsWith("@STD_"));

Once you have the list in this enumerable type it is much easier to look for missing keys.

  • Related