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 6710089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:57:35+00:00 2026-05-26T07:57:35+00:00

Code I think can be Linq: foreach (var key in _dic.Keys) // for each

  • 0

Code I think can be Linq:

    foreach (var key in _dic.Keys) // for each observed key
        if (_changedKeys.Contains(key)) // if it changed
            foreach (var item in _dic[key]) // then for each observer
                if (_webSitePage.Session[key] != null) // , as long as the value is something
                    item(_webSitePage.Session[key]); // call the registered delegate

In context of surrounding code:

public class WebSiteContext
{
    private WebSitePage _webSitePage;

    internal void SetSessionValue<T>(string key, T value)
    {
        object current = _webSitePage.Session[key];
        if (current != null && current.Equals(value)) return;
        _webSitePage.Session[key] = value;
        _changedKeys.Add(key);
    }

    Dictionary<string, List<Action<object>>> _dic = new Dictionary<string, List<Action<object>>>();

    List<string> _changedKeys = new List<string>();

    internal void WhenSessionValueChanges(string key, Action<object> action)
    {
        if (!_dic.ContainsKey(key)) _dic[key] = new List<Action<object>>(); // create on demand
        _dic[key].Add(action);
    }

    internal void PageLoadComplete()
    {
        foreach (var key in _dic.Keys) // for each observed key
            if (_changedKeys.Contains(key)) // if it changed
                foreach (var item in _dic[key]) // then for each observer
                    if (_webSitePage.Session[key] != null) // , as long as the value is something
                        item(_webSitePage.Session[key]); // call the registered delegate

    }
}

public class WebSitePage : System.Web.UI.Page
{
    public WebSitePage()
    {
        this.WebSiteContext = new WebSiteContext(this);
    }

    public WebSiteContext WebSiteContext { get; set; }

    protected override void OnLoadComplete(EventArgs e)
    {
        base.OnLoadComplete(e);
        this.WebSiteContext.PageLoadComplete();
    }
}
  • 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-26T07:57:35+00:00Added an answer on May 26, 2026 at 7:57 am

    It could be changed to LINQ, but since you are calling item(...) for its side-effects, I think using LINQ here is inappropriate and a foreach loop is better.

    You could however write something like this:

     foreach (var key in _dic.Keys.Where(key => _changedKeys.Contains(key))
     {
         foreach (var item in _dic[key])
         {
             var value = _webSitePage.Session[key];
             if (value != null)
             {
                 item(value);
             }
         }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

God I hate the term code smell, but I can't think of anything more
The code below shows me (I think) that the for each loop is about
I'm refactoring this code and was trying to think of a simple linq expression
One thing I lack understanding on is how can code compiled for an Intel
i can't understand one thing. In code, for example: $filePath = 'http://wwww.server.com/file.flv'; if( file_exist($filePath)
Best explained with code I think, this is just a simple example: public class
I'm trying to code what I think is a fairly routine AJAX pattern using
I have some code which I think has extra release statements. Is the code
I think back to Joel Spolsky's article about never rewriting code from scratch. To
This code in JS gives me a popup saying i think null is a

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.