Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7569145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:05:58+00:00 2026-05-30T15:05:58+00:00

I have a collection of items that each have an IObserver, and a message

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:05:59+00:00Added an answer on May 30, 2026 at 3:05 pm

    Try this:

    IObservable<Message> source;
    foreach(item in items)
    {
       var item2 = item;
       var filtered = from msg in source
                  where msg.Id == item2.Id
                  selct msg;
       filtered.Subscribe(item2.Sink);
    }
    

    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 item and 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a collection of items (big rationals) that I'll be processing. In each
I have a list view that displays a collection of items, each item has
I have a collection that contains all the items that I want to keep
I have Backbone.js collection that holds (for example) 30 items. I want to pass
I have a collection of items in an unordered list that overlap each other.
I have a collection of Items that each have a collection of Relationships. I
I have a treeview that is bound to a collection and each item in
Do Java collections have a built-in method to return multiple items from that collection?
I have a private class variable that holds a collection of order items: Private
I have a List<NameClass> that stores a collection of NameClass items with a property

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.