Avoid double control search in LINQ query
I have a 开发者_C百科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.
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.
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; });
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
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
精彩评论