If the statement is in the lambda function?

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; } 
+4
source share
5 answers

Mquander's solution can be slightly improved to reduce duplication. You can use the fact that you can pass a null element in the contents of the XElement constructor and ignore it. Therefore, we can move the condition further:

 Func<ErrorType, XElement> recursiveGenerator = null; recursiveGenerator = (error => new XElement(error.Name, outputTagsOnly ? null : new XAttribute("Ignore", error.Filter), error.ChildErrors.Select(recursiveGenerator)); var element = new XElement("ErrorList", ChildErrors.Select(recursiveGenerator)); 
+6
source

You can move the if if statement inside the lambda function safely if you prefer:

 Func<ErrorType, XElement> recursiveGenerator = null; recursiveGenerator = (error => outputTagsOnly ? new XElement(error.Name, error.ChildErrors.Select(recursiveGenerator)); : new XElement(error.Name, new XAttribute("Ignore", error.Filter), error.ChildErrors.Select(recursiveGenerator))); var element = new XElement("ErrorList", ChildErrors.Select(recursiveGenerator)); 

Other than that, there seems to be no easy way to simplify what you have.

(PS When it looks ugly, apply lipstick on this pig with a beautiful seal;)

+6
source

You can easily make a decision between values ​​of the same type in lambda:

 customer => flag ? customer.Name : customer.Address 

You can use the if statement in lambda with little effort:

 customer => { if (flag) return customer.Name else return customer.Address } 

None of them help your method significantly.

+5
source

I think you can do it, but at the end of the day it’s all the same if:

  recursiveGenerator = error => outputTagsOnly ? new XElement(error.Name,error.ChildErrors.Select(recursiveGenerator) : new XElement(error.Name,new XAttribute("Ignore", error.Filter), error.ChildErrors.Select(recursiveGenerator); 
0
source

You can try to decompose your problem into two different ones:

  • How to build a tree from an error structure.
  • What to put in the nodes of the tree.

Then the code will look like this:

  private XElement ErrorListToXml(ErrorList el, bool outputTagsOnly) { // Need to declare in advance to call within the lambda. Func<ErrorType, XElement> treeGenerator = null; Func<ErrorType, object[]> elementParametersGenerator = null; treeGenerator = error => new XElement (error.Name, elementParametersGenerator(error)); if(outputTagsOnly) elementParametersGenerator = error => new object[] {error.ChildErrors.Select(treeGenerator)}; else elementParametersGenerator = error => new object[] { new XAttribute("Ignore", error.Filter), error.ChildErrors.Select(treeGenerator) }; var element = new XElement ("ErrorList", ChildErrors.Select(treeGenerator)); Console.WriteLine(element); return element; } 

In this particular case, not much better, but this is a more general approach.

0
source

Source: https://habr.com/ru/post/1285670/


All Articles