Reputation: 27384
This may be quite simple but I'm rather new to Lambda's so bear with me.
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.
The function is designed to write out a custom class to XML - I think the code is pretty self explanitory.
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?
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;
}
Upvotes: 9
Views: 31307
Reputation: 1499800
mquander's solution can be improved slightly to reduce duplication. You can use the fact that you can pass in null
an element in the XElement constructor content, and it gets ignored. We can therefore move the condition further in:
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));
Upvotes: 8
Reputation: 16980
You can try to decompose your problem into two different ones:
Then the code will look like:
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;
}
Not any significantly better in this particular case, but it's a more general approach.
Upvotes: 0
Reputation: 110071
You can make a decision between values of the same type in a lambda pretty easily:
customer => flag ? customer.Name : customer.Address
You can use an if statement in a lambda with a little more effort:
customer =>
{
if (flag)
return customer.Name
else
return customer.Address
}
Neither of these helps your method greatly.
Upvotes: 10
Reputation: 103742
I guess you can do this, but end of the day it's still an 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);
Upvotes: 0
Reputation: 71937
You could move the "if" statement inside the lambda function safely, if you preferred:
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 doesn't seem to be any trivial way to simplify what you've got.
(P.S. When it looks ugly, put some lipstick on that pig by pretty-printing it ;)
Upvotes: 6