title: If statement within Lambda function?
body: <p>This may be quite simple but I'm rather new to Lambda's so bear with me.</p>
  
  <p>I have a function that uses a Lambda function to recurse.
  The main function receives a bool telling it to include certain information or not within the lambda.</p>
  
  <p>The function is designed to write out a custom class to XML - I think the code is pretty self explanitory.</p>
  
  <p>At the moment I have overcome the problem using a simple if statement, but it feels ugly so wondered if anyone knew a better way?</p>
  
  <pre><code>        private XElement ErrorListToXml(ErrorList el, bool outputTagsOnly)
      {
          // Need to declare in advance to call within the lambda.
          Func&lt;ErrorType, XElement&gt; recursiveGenerator = null;
  
          if (outputTagsOnly)
              recursiveGenerator = error =&gt; new XElement
                  (error.Name,
                   error.ChildErrors.Select(recursiveGenerator));
          else
              recursiveGenerator = error =&gt; new XElement
            (error.Name,
            new XAttribute("Ignore", error.Filter),
             error.ChildErrors.Select(recursiveGenerator));
  
  
          var element = new XElement
                     ("ErrorList",
                      ChildErrors.Select(recursiveGenerator));
  
          Console.WriteLine(element);
  
          return element;
      }
  </code></pre>