September 29th, 2011, 04:10 AM
#1
Extract a portion of sorted dictionary into an array
Hi,
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?
Thanks!!
September 29th, 2011, 11:14 AM
#2
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...
Code:
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());
September 29th, 2011, 09:04 PM
#3
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
Forum Rules
Click Here to Expand Forum to Full Width
Bookmarks