我可以/应该 Linqify 这个 foreach/if/foreach/if/invoke 代码吗?

发布于 2024-12-11 07:04:56 字数 2025 浏览 0 评论 0原文

我认为代码可以是 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

在周围代码的上下文中:

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();
    }
}

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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝海似她心 2024-12-18 07:04:56

可以更改为 LINQ,但由于您调用 item(...) 是为了产生副作用,我认为在这里使用 LINQ 是不合适的,并且 foreach 循环更好。

不过你可以这样写:

 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);
         }
     }
 }

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);
         }
     }
 }
¢好甜 2024-12-18 07:04:56
var keys = from key in _dic.Keys
           where _changedKeys.Contains(key) && _webSitePage.Session[key] != null
           from item in _dic[key]
           select item;

foreach (var key in keys)
    item(_webSitePage.Session[key]);

编辑:

以上是严重错误的。抱歉。我打算使用查询理解来回答这个问题。这是一个尝试:

var tuples = from key in _dic.Keys
       where _changedKeys.Contains(key) && _webSitePage.Session[key] != null
       from item in _dic[key]
       select new { key, item };

foreach (var t in tuples)
    t.item(_webSitePage.Session[t.key]);
var keys = from key in _dic.Keys
           where _changedKeys.Contains(key) && _webSitePage.Session[key] != null
           from item in _dic[key]
           select item;

foreach (var key in keys)
    item(_webSitePage.Session[key]);

EDIT:

The above is horribly wrong. Apologies. I was intent on using query comprehension to answer the question. Here's an attempt:

var tuples = from key in _dic.Keys
       where _changedKeys.Contains(key) && _webSitePage.Session[key] != null
       from item in _dic[key]
       select new { key, item };

foreach (var t in tuples)
    t.item(_webSitePage.Session[t.key]);
小…红帽 2024-12-18 07:04:56

这会缩短一点。请注意,我移动了 _webSitePage 检查,因为它不依赖于 item 的值:

foreach (var kvp in _dic.Where(kvp => !_changedKeys.Contains(kvp.Key) && _webSitePage.Session[kvp.Key] != null))
    foreach (var item in kvp.Values) // then for each observer
        item(_webSitePage.Session[key]); // call the registered delegate

This shortens it a little. Note that I moved the _webSitePage check up because it does not depend on the value of item:

foreach (var kvp in _dic.Where(kvp => !_changedKeys.Contains(kvp.Key) && _webSitePage.Session[kvp.Key] != null))
    foreach (var item in kvp.Values) // then for each observer
        item(_webSitePage.Session[key]); // call the registered delegate
财迷小姐 2024-12-18 07:04:56
var keys = _dic.Keys.Where(key => _changedKeys.Contains(key) && 
                                  (_webSitePage.Session[key] != null));
foreach (var key in keys)
  foreach (var item in _dic[key])
    item(_webSitePage.Session[key]);

(第二个测试不需要在内部 foreach 循环中,除非 item 修改 _webSitePage.Session)

var keys = _dic.Keys.Where(key => _changedKeys.Contains(key) && 
                                  (_webSitePage.Session[key] != null));
foreach (var key in keys)
  foreach (var item in _dic[key])
    item(_webSitePage.Session[key]);

(the second test does not need to be in the inner foreach loop unless item modifies _webSitePage.Session)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文