避免 LINQ 查询中的双重控制搜索

发布于 2024-09-07 03:39:17 字数 737 浏览 1 评论 0原文

我有一个 Dictionary 其中键 - 控件的 ID 和值 - 要设置的可见状态:

var dic = new Dictionary<string, bool>
{
    { "rowFoo", true},
    { "rowBar", false },
    ...
};

某些控件可以为 null,即 dic。 ToDictionary(k => this.FindControl(k), v => v) 将不起作用,因为 key 不能为 null。

我可以做下一步:

dic
    .Where(p => this.FindControl(p.Key) != null)
    .ForEach(p => this.FindControl(p.Key).Visible = p.Value); // my own extension method

但这将为每个键调用 FindControl() 两次。

如何避免双重搜索并仅选择那些存在适当控制的键?

类似于:

var c= FindControl(p.Key);
if (c!= null)
    return c;

但是使用 LINQ。

I have a Dictionary<string, bool> where key - control's ID and value - it's visible status to set:

var dic = new Dictionary<string, bool>
{
    { "rowFoo", true},
    { "rowBar", false },
    ...
};

Some of controls can be null, i.e. dic.ToDictionary(k => this.FindControl(k), v => v) will not work because key can't be null.

I can do next:

dic
    .Where(p => this.FindControl(p.Key) != null)
    .ForEach(p => this.FindControl(p.Key).Visible = p.Value); // my own extension method

but this will call FindControl() twice for each key.

How to avoid double search and select only those keys for which appropriate control exists?

Something like:

var c= FindControl(p.Key);
if (c!= null)
    return c;

but using LINQ.

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

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

发布评论

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

评论(4

一身仙ぐ女味 2024-09-14 03:39:17
dic.Select(p => new { Control = this.FindControl(p.Key), p.Value })
   .Where(p => p.Control != null)
   .ForEach(p => p.Control.Visible = p.Value);

...但我只是将 foreachif 语句一起使用。不要过度使用 LINQ。

dic.Select(p => new { Control = this.FindControl(p.Key), p.Value })
   .Where(p => p.Control != null)
   .ForEach(p => p.Control.Visible = p.Value);

... but I'd simply use foreach with an if statement. Don't overuse LINQ.

逆光下的微笑 2024-09-14 03:39:17
dic
 .Select(kvp => new { Control = this.FindControl(kvp.Key), Visible = kvp.Value })
 .Where(i => i.Control != null)
 .ToList()
 .ForEach(p => { p.Control.Visible = p.Visible; });
dic
 .Select(kvp => new { Control = this.FindControl(kvp.Key), Visible = kvp.Value })
 .Where(i => i.Control != null)
 .ToList()
 .ForEach(p => { p.Control.Visible = p.Visible; });

看,没有匿名实例(不过也好不了多少,新建组并枚举两次)

IEnumerable<IGrouping<bool, Control>> visibleGroups = 
  from kvp in controlVisibleDictionary
  let c = this.FindControl(kvp.Key)
  where c != null
  group c by kvp.Value;

foreach(IGrouping<bool, Control> g in visibleGroups)
{
  foreach(Control c in g)
  {
    c.Visible = g.Key;
  }
}
  • 免责声明,不像 foreach-if 那么简单

Look, no anonymous instances (hardly better though, newing-up groups and enumerating twice)

IEnumerable<IGrouping<bool, Control>> visibleGroups = 
  from kvp in controlVisibleDictionary
  let c = this.FindControl(kvp.Key)
  where c != null
  group c by kvp.Value;

foreach(IGrouping<bool, Control> g in visibleGroups)
{
  foreach(Control c in g)
  {
    c.Visible = g.Key;
  }
}
  • Disclaimer, not as simple as a foreach-if
预谋 2024-09-14 03:39:17

与 David 的想法相同,但在 一个 字符串中:

(from p in new System.Collections.Generic.Dictionary<string, bool>
{
    { "rowAddress", value },
    { "rowTaxpayerID", !value },
    { "rowRegistrationReasonCode", !value },
    { "rowAccountID", !value },
    { "rowAccountIDForeign", value },
    { "rowBankAddress", value },
    { "rowBankID", !value },
    { "rowBankSwift", value },
    { "rowBankAccountID", !value }
}
let c = this.FindControl(p.Key)
where c != null
select new // pseudo KeyValuePair
{
    Key = c,
    Value = p.Value
}).ForEach(p => p.Key.Visible = p.Value); // using own ext. method

The same idea then David's but in one string:

(from p in new System.Collections.Generic.Dictionary<string, bool>
{
    { "rowAddress", value },
    { "rowTaxpayerID", !value },
    { "rowRegistrationReasonCode", !value },
    { "rowAccountID", !value },
    { "rowAccountIDForeign", value },
    { "rowBankAddress", value },
    { "rowBankID", !value },
    { "rowBankSwift", value },
    { "rowBankAccountID", !value }
}
let c = this.FindControl(p.Key)
where c != null
select new // pseudo KeyValuePair
{
    Key = c,
    Value = p.Value
}).ForEach(p => p.Key.Visible = p.Value); // using own ext. method
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文