I have a collection of items that each have an IObserver, and a message source that produces Messages(!) I want to filter the messages, and only send on the relevant ones to each item. I think this is a good fit for System.Reactive’s linq. Below is an example:
IObservable<Message> source;
foreach(item in items)
{
var filtered = from msg in source
where msg.Id == item.Id
selct msg;
filtered.Subscribe(item.Sink);
}
The problem is that when the source produces a message, the query is evaluated with respect to the last item in the loop. So if there are twenty items each query will be against the properties of item 20.
How do I fix this?
Try this:
I think that might help. All is because of scoping I think.
Here is what I think happen in your code. Since the linq query is evaluated lazy the query will always use the variable
itemand when you enter the loop the variable was last set to the latest item in the collection. By assigning the value to another value in the beginning of the loop each query will work against that variable instead which will not changed in each iteration.