如何在linq语句中添加if语句

时间:2012-08-26 00:44:39

标签: c#

有人可以帮我添加下面代码片段中注释的if语句吗?

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}
    );
}

2 个答案:

答案 0 :(得分:4)

只需要添加花括号:

testDictionary.Keys.ToList().ForEach(k => 
{
    //if(true){add toReturn list}
});

答案 1 :(得分:3)

或添加Where子句

testDictionary
  .Keys
  .Where(k => conditional(k))
  .ForEach(k => { /* something else */ } );