views:

49

answers:

4

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.

+3  A: 
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.

Mehrdad Afshari
+2  A: 
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; });
STO
+1  A: 

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
David B
A: 

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
abatishchev