Linq to Hashtable
-
Hey guys I have a HashTable that uses a date time as its key and a class for the value. Now using linq i need to get all the keys from September 2008 for example, but im stumped as how to do this. im lost trying to get the query expression to query the HashTable, any ideas? Thanks
Harvey Saayman - South Africa Junior Developer .Net, C#, SQL
you.suck = (you.passion != Programming)
-
Hey guys I have a HashTable that uses a date time as its key and a class for the value. Now using linq i need to get all the keys from September 2008 for example, but im stumped as how to do this. im lost trying to get the query expression to query the HashTable, any ideas? Thanks
Harvey Saayman - South Africa Junior Developer .Net, C#, SQL
you.suck = (you.passion != Programming)
Harvey, A Hashtable is non-generic (and thus, will box your DateTime values). This characteristic makes it difficult to work with LINQ, which operates on generic types. Is there a reason you're using Hashtable instead of Dictionary<DateTime, MyClass>? If you have a good reason to continue using the deprecated Hashtable, here's how to use LINQ with it:
Hashtable t = new Hashtable();
t.Add(DateTime.Now, "foo");
t.Add(DateTime.Now.AddDays(2), "baz");var stringsFromToday = from element in t.Cast<DictionaryEntry>()
let time = (DateTime)element.Key
where time.Day == DateTime.Now.Day
select (string)element.Value;Tech, life, family, faith: Give me a visit. I'm currently blogging about: Upon this disciple I'll build my new religion? The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
Harvey, A Hashtable is non-generic (and thus, will box your DateTime values). This characteristic makes it difficult to work with LINQ, which operates on generic types. Is there a reason you're using Hashtable instead of Dictionary<DateTime, MyClass>? If you have a good reason to continue using the deprecated Hashtable, here's how to use LINQ with it:
Hashtable t = new Hashtable();
t.Add(DateTime.Now, "foo");
t.Add(DateTime.Now.AddDays(2), "baz");var stringsFromToday = from element in t.Cast<DictionaryEntry>()
let time = (DateTime)element.Key
where time.Day == DateTime.Now.Day
select (string)element.Value;Tech, life, family, faith: Give me a visit. I'm currently blogging about: Upon this disciple I'll build my new religion? The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
Judah Himango wrote:
Is there a reason you're using Hashtable instead of Dictionary
none whatsoever, ill check this out tomorrow morning :) thanx for the help
Harvey Saayman - South Africa Junior Developer .Net, C#, SQL
you.suck = (you.Passion != Programming & you.Occupation == jobTitles.Programmer)
1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111 -
Judah Himango wrote:
Is there a reason you're using Hashtable instead of Dictionary
none whatsoever, ill check this out tomorrow morning :) thanx for the help
Harvey Saayman - South Africa Junior Developer .Net, C#, SQL
you.suck = (you.Passion != Programming & you.Occupation == jobTitles.Programmer)
1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111With the generic dictionary, it's quite a bit better story, both performance-wise and readability:
var dictionary = new Dictionary<DateTime, string>()
{
{ DateTime.Now, "now" },
{ DateTime.Now.AddDays(2), "two days from now" }
};// Get all keys from September 2008
var result = from key in dictionary.Keys
where key.Month == 9 && key.Year == 2008
select key;Tech, life, family, faith: Give me a visit. I'm currently blogging about: Upon this disciple I'll build my new religion? The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
With the generic dictionary, it's quite a bit better story, both performance-wise and readability:
var dictionary = new Dictionary<DateTime, string>()
{
{ DateTime.Now, "now" },
{ DateTime.Now.AddDays(2), "two days from now" }
};// Get all keys from September 2008
var result = from key in dictionary.Keys
where key.Month == 9 && key.Year == 2008
select key;Tech, life, family, faith: Give me a visit. I'm currently blogging about: Upon this disciple I'll build my new religion? The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
worked like a charm :) thanx bud!
Harvey Saayman - South Africa Junior Developer .Net, C#, SQL
you.suck = (you.Passion != Programming & you.Occupation == jobTitles.Programmer)
1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111