Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hashtable Filter values 1

Status
Not open for further replies.

DotNetGnat

Programmer
Mar 10, 2005
5,548
IN
Guys,

I have a hashtable that looks

Key | Value
____________
1_1 | myobject1
1_2 | myobject2
2_1 | myobject3
2_2 | myobject4
2_3 | myobject5

I want to filter this hashtable based on the key 1* or 2*
and create a sub list/arraylist of myobjects

some method like FilteredList(2_*) would give an arraylist of 3 objects.

thanks in advance.

-DNG


 
hashtables are not intended for query/search/filter. they are meant to retrieve an object by a key.

You can work around this limitation, but I wouldn't recommend this as a standard practice.

option 1. loop through the keys and return objects with keys that match the pattern.
Code:
var hash = new Hashtable();
var pattern = "1_";
foreach(string key in hash.Keys)
{
  if (key.StartsWith(pattern) == false) continue;
  yield return hash[key];
}
another approach is to nest hashtables.
Code:
private readonly HashTable hash = new Hashtable();

public void Add(int key1, int key2, object obj)
{
   if(hash.ContainsKey(key1) == false)
   {
       hash.Add(key1, new Hashtable());
   }
   var inner = (Hashtable)hash[key1];
   if(inner.ContainsKey(key2) == false)
   {
      inner.Add(key2, null);
   }
   inner[key2] = obj;
}
public object Get(int key1, int key2)
{
   if(hash.ContainsKey(key1) == false)
   {
       return null;
   }
   var inner = (Hashtable)hash[key1];
   if(inner.ContainsKey(key2) == false)
   {
      return null;
   }
   return inner[key2];
}

public object[] GetAll(int key)
{
   if(hash.ContainsKey(key1) == false)
   {
       return new object[0];
   }
   var inner = (Hashtable)hash[key1];
   return inner.Values.Cast<object>().ToArray();
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Jason,

Thanks again. What do you suggest for working with query/search/filter.

I will try the approaches that you mentioned and will get back to you.

-DNG
 
depending on the amount, frequency as persistence model (document db, rdbms, memory, text, web service, etc) you have some options.
1. in memory filtering (linq, datatable/dataview)
2. lucene.net (in conjunction with an rdbms or document database)
3. full text indexing (sql server)
4. raw sql (select * from table where column1 = @this and column2=@that)
5. raw sql (select * from table where column like '%something%')

option 1 works for small sets of data or if the data source (like a web service) doesn't provide a query API.
option 2 works in a two step phase.
1. query lucene to get the keys
2. query the persistent store by the keys from lucene
option 3 only works on sql server (I would assume Oracle has a similar feature thou)
option 4 is very common. by indexing columns 1 and 2 you get a good execution plan.
option 5 is the worst way to query a database. it will use a full table scan.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
wonderful information. Thanks much.

Your post definitely deserves a star...

-DNG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top