It might be pretty simple, but I'm pretty new to lambda, so bear with me.
I have a function that uses the Lambda function for recursion. The main function gets a bool, indicating that it includes certain information or not in lambda.
The function is designed to write out a custom class in XML - I think the code explains it pretty well.
Right now I have overcome the problem with a simple if statement, but it seems to me ugly so wondering if anyone knows a better way?
private XElement ErrorListToXml(ErrorList el, bool outputTagsOnly) { // Need to declare in advance to call within the lambda. Func<ErrorType, XElement> recursiveGenerator = null; if (outputTagsOnly) recursiveGenerator = error => new XElement (error.Name, error.ChildErrors.Select(recursiveGenerator)); else recursiveGenerator = error => 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; }
Chris source share