C# Lambda 函数中的 if 语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/944853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
If statement within Lambda function?
提问by Chris
This may be quite simple but I'm rather new to Lambda's so bear with me.
这可能很简单,但我对 Lambda 很陌生,所以请耐心等待。
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.
我有一个使用 Lambda 函数进行递归的函数。main 函数接收一个布尔值,告诉它是否在 lambda 中包含某些信息。
The function is designed to write out a custom class to XML - I think the code is pretty self explanitory.
该函数旨在为 XML 写出一个自定义类 - 我认为该代码是不言自明的。
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?
目前我已经使用一个简单的 if 语句解决了这个问题,但是感觉很丑,所以想知道是否有人知道更好的方法?
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;
}
采纳答案by Jon Skeet
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:
mquander 的解决方案可以稍微改进以减少重复。您可以使用这样一个事实,即您可以null
在 XElement 构造函数内容中传入一个元素,但它会被忽略。因此,我们可以进一步移动条件:
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));
回答by mqp
You could move the "if" statement inside the lambda function safely, if you preferred:
如果您愿意,您可以安全地在 lambda 函数内移动“if”语句:
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 ;)
(PS 当它看起来很丑时,通过漂亮的印刷给那只猪涂上口红;)
回答by BFree
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);
回答by Amy B
You can make a decision between values of the same type in a lambda pretty easily:
您可以很容易地在 lambda 中的相同类型的值之间做出决定:
customer => flag ? customer.Name : customer.Address
You can use an if statement in a lambda with a little more effort:
您可以稍加努力地在 lambda 中使用 if 语句:
customer =>
{
if (flag)
return customer.Name
else
return customer.Address
}
Neither of these helps your method greatly.
这些都没有对您的方法有很大帮助。
回答by Max Galkin
You can try to decompose your problem into two different ones:
您可以尝试将您的问题分解为两个不同的问题:
- How to build a tree from errors structure.
- What to put into the tree nodes.
- 如何从错误结构构建树。
- 将什么放入树节点。
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.
在这种特殊情况下并没有明显更好,但它是一种更通用的方法。