Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Linq to Hashtable

Linq to Hashtable

Scheduled Pinned Locked Moved LINQ
csharpdatabasetutoriallinqquestion
5 Posts 2 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Harvey Saayman
    wrote on last edited by
    #1

    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)

    J 1 Reply Last reply
    0
    • H Harvey Saayman

      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)

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      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

      H 1 Reply Last reply
      0
      • J Judah Gabriel 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

        H Offline
        H Offline
        Harvey Saayman
        wrote on last edited by
        #3

        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

        J 1 Reply Last reply
        0
        • H Harvey Saayman

          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

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #4

          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

          H 1 Reply Last reply
          0
          • J Judah Gabriel 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

            H Offline
            H Offline
            Harvey Saayman
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups