问题: 按照指定的规则从一个消息队列中检索消息
解答:
使用 System.Messaging.MessageQueue 类型编写一个队列, 通过 LINQ 来检索消息.
示例代码:
string queuePath=@".\private$\LINQMQ";
MessageQueue messageQueue = new MessageQueue(queuePath);
binaryMessageFormatter messageFormatter = new BinaryMessageFormatter();
var query = from Message msg in messageQueue
where ((msg.Formatter =messageFormatter)==messageFormatter)
&& int.Parse(msg.Label)> 5
&& msg.Body.ToString().Contains('D')
orderby msg.Body.ToString() descending
select msg;
foreach( var msg in query)
{
Console.WriteLine("Label: " + msg.Label + " Body: " + msg.Body);
}
讨论:
LINQ 关键字:
var, from, where, orderby, select