1

How can I add the if statement that's commented in the code snippet below?

Dictionary<string,int> toReturn;
List<string> lookup = GetIt();

foreach (string test in lookup)
{
    var testDictionary = ToGroupDictionary(test);
    testDictionary.Keys.ToList().ForEach(k =>
        //if(true){add toReturn list}
    );
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Rod
  • 14,529
  • 31
  • 118
  • 230
  • You would be better off just using a `foreach` loop -- [`.ForEach` is virtually a deprecated construct](http://stackoverflow.com/questions/10299458/is-the-listt-foreach-extension-method-gone). – Kirk Woll Aug 26 '12 at 01:55

2 Answers2

4

Just need to add curly braces:

testDictionary.Keys.ToList().ForEach(k => 
{
    //if(true){add toReturn list}
});
Gabe
  • 49,577
  • 28
  • 142
  • 181
3

Or add a Where clause

testDictionary
  .Keys
  .Where(k => conditional(k))
  .ForEach(k => { /* something else */ } );
Marc Bollinger
  • 3,109
  • 2
  • 27
  • 32
  • 1
    Except for `ForEach` - `IEnumerable` doesn't support that extension. You do require `ToList` in this case. – Enigmativity Aug 26 '12 at 01:39
  • *sigh* And that's why we don't open our mouths without double-checking. Side note, while that makes sense (I knew there was a reason I wasn't intimately familiar with the `ForEach` extension), this provides a good justification for _why_ it doesn't support that method - http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx – Marc Bollinger Aug 26 '12 at 07:37