In a project that I'm currently working on, I needed to retrieve messages from MSMQ based on some criteria, I used LINQ to do that and would like to share this code with you.
MessageQueue messageQueue = new MessageQueue(Constants.MessageQueueName);
messageQueue.Formatter = new BinaryMessageFormatter();
var TrackingRecords = from System.Messaging.Message message in messageQueue
where message.Label.Contains(Constants.MessageLabel)
select (ICLGRecord)message.Body;
// Itterate through the Messages
foreach (var TrackingRecord in TrackingRecords)
{
}
// Remove All Items From MSMQ
messageQueue.Purge();