CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2002

    Extract a portion of sorted dictionary into an array


    How can extract a portion of a large dictionary<DateTime, string>, where DateTime is sorted? For example, i want to extarct all strings occur between 2010/10/01 to 2011/10/01 into an array, sorted either in time ascending or decending order? I was looking at sorteddictionary, but i just can't understand how to use it to achieve my purpose? can show me some codes?


  2. #2
    Join Date
    May 2011
    Washington State

    Re: Extract a portion of sorted dictionary into an array

    This seems to work... there may be a better/more efficient way, I just don't know of it...

                Dictionary<DateTime, string> SampleDict = new Dictionary<DateTime, string>();
                SampleDict.Add(DateTime.Parse("10/1/2011"), "Alpha");
                SampleDict.Add(DateTime.Parse("10/20/2011"), "Bravo");
                SampleDict.Add(DateTime.Parse("10/13/2011"), "Charlie");
                SampleDict.Add(DateTime.Parse("10/22/2011"), "Delta");
                SampleDict.Add(DateTime.Parse("10/5/2011"), "Echo");
                var subSet = SampleDict.Where(item => item.Key > DateTime.Parse("10/1/2011") && item.Key < DateTime.Parse("10/22/2011")).OrderBy(item => item.Key);
                Dictionary<DateTime,string> results = subSet.ToDictionary(item => item.Key, item => item.Value);
                foreach (DateTime key in results.Keys)
                    Console.WriteLine("{0} : {1}", key.ToShortDateString(), results[key].ToString());

  3. #3
    Join Date
    Jul 2002

    Re: Extract a portion of sorted dictionary into an array

    Thanks fcronin!

    Anyone else has a solution, probably using the sorteddictionary or sortedlist class? I am keen to know if there is a soultion without LINQ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)