ConcurrentDictionary extension method OrderBy?

I'm attempting to use this extension method to get the earliest item from the ConcurrentDictionary, based on 'RunDate'.

I need to process the items in the ConcurrentDictionary in ascending order based on the 'RunDate'.

I was planning to use the OrderBy along with Take1 to get this item to process, but I'm not getting my topOne var to return anything but null.

Is my approach feasible?  If so, if you have sample of how to perform this with C#, I would appreciate it.


private ConcurrentDictionary<Guid, POptions> PQueue = new ConcurrentDictionary<Guid, POptions>();

//Add items to this dictionary - initiated from a UI
PQueue.TryAdd(_currentPOptions.newGuid, _currentPOptions)

//Timer process to process the earliest 'RunDate' item from the ConcurrentDictionary
//I get null values returned by this line of code, but I know there are multiple items in the ConcurrentDictionary
var topOne = PQueue.OrderBy(q => q.Value.RunDate).ToList().Take(1);
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Works for me.  Validate that your collection or the items in your collection are not null.

Proof of concept -
using System;
using System.Collections.Concurrent;
using System.Linq;

namespace EE_Q28711401
	class Program
		static ConcurrentDictionary<Guid, Option> queue = new ConcurrentDictionary<Guid, Option>();

		static void Main(string[] args)
			for (int i = 0; i < 1000; i++)
				Option temp = new Option();
				if (i % 5 == 0)
					temp.RunDate = DateTime.Now.AddDays(-5);
				else if (i % 4 == 0)
					temp.RunDate = DateTime.Now.AddDays(-4);
				else if (i % 3 == 0)
					temp.RunDate = DateTime.Now.AddDays(-3);
				else if (i % 2 == 0)
					temp.RunDate = DateTime.Now.AddDays(-2);
					temp.RunDate = DateTime.Now.AddDays(-1);
				queue.TryAdd(temp.Guid, temp);

			var top = queue.OrderBy(x => x.Value.RunDate).Take(1);
			foreach (var pair in top)
				Console.WriteLine("{0} = {1}", pair.Key, pair.Value.RunDate.ToShortDateString());

	class Option
		private Guid fGuid = Guid.Empty;
		public Guid Guid
				if (fGuid.Equals(Guid.Empty))
					fGuid = Guid.NewGuid();
				return fGuid; 
			set { fGuid = value; }

		public DateTime RunDate { get; set; }

Open in new window

Produces the following output -Capture.JPG-saige-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Miguel OzSoftware EngineerCommented:
Could you show the rest of your code?
The code seems straight forwards, but notice that PQueue  is a private field and I am not sure if your code is calling to the same instance in both UI and timer event.
JBM2015Author Commented:
Thanks guys, sorry for the delay, I was on vacation.

Here's what I ended up using, I added another column to the dictionary to get a unique counter value (IntUnique):

var unboundContainer = PQueue.OrderBy(kvp => kvp.Value.RunDate).ThenBy(kvp2 => kvp2.Value.IntUnique).ToList();
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.