我可以/应该 Linqify 这个 foreach/if/foreach/if/invoke 代码吗?
我认为代码可以是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它可以更改为 LINQ,但由于您调用
item(...)
是为了产生副作用,我认为在这里使用 LINQ 是不合适的,并且foreach
循环更好。不过你可以这样写:
It could be changed to LINQ, but since you are calling
item(...)
for its side-effects, I think using LINQ here is inappropriate and aforeach
loop is better.You could however write something like this:
编辑:
以上是严重错误的。抱歉。我打算使用查询理解来回答这个问题。这是一个尝试:
EDIT:
The above is horribly wrong. Apologies. I was intent on using query comprehension to answer the question. Here's an attempt:
这会缩短一点。请注意,我移动了 _webSitePage 检查,因为它不依赖于
item
的值:This shortens it a little. Note that I moved the _webSitePage check up because it does not depend on the value of
item
:(第二个测试不需要在内部 foreach 循环中,除非 item 修改 _webSitePage.Session)
(the second test does not need to be in the inner foreach loop unless item modifies _webSitePage.Session)