C# Linq if/else 条件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15909926/
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
Linq if/else condition?
提问by Sparhawk
I know this will probably be a newbie question. Is there a way to choose different search criteria depending on the bool value? Later in the code, I want to loop through the object (alDisabledPrograms). I know the if/else is not correct, I put that in there to show how I'd like that to be handled. I attempted to place this inside a larger if/else condition but was unable to loop through alDisabledPrograms later. Thoughts?
我知道这可能是一个新手问题。有没有办法根据布尔值选择不同的搜索条件?在代码的后面,我想遍历对象 (alDisabledPrograms)。我知道 if/else 不正确,我把它放在那里以表明我希望如何处理它。我试图将它放在一个更大的 if/else 条件中,但后来无法通过 alDisabledPrograms 循环。想法?
var alDisabledPrograms = xlServerRoles.Descendants("ServerRole")
if(isDup)
{
.Where(dp => dp.Element("ServerType").Value == currentColumn.Substring(0, currentColumn.Length - 1))
}
else
{
.Where(dp => dp.Element("ServerType").Value == currentColumn)
}
.Descendants("ProgramName")
.Select(p => p.Value)
.ToList();
采纳答案by Jon Skeet
With your particular code, the answer is really simple:
使用您的特定代码,答案非常简单:
string targetColumn = isDup ? currentColumn.Substring(0, currentColumn.Length - 1)
: currentColumn;
var alDisabledPrograms = xlServerRoles.Descendants("ServerRole")
.Where(dp => dp.Element("ServerType").Value == targetColumn)
.Descendants("ProgramName")
.Select(p => p.Value)
.ToList();
In general though, to apply very different queries, you could either use:
但总的来说,要应用非常不同的查询,您可以使用:
IEnumerable<XElement> roles = xlServerRoles.Descendants("ServerRole");
if (isDup)
{
roles = roles.Where(dp => ...);
}
else
{
roles = roles.Where(dp => ...);
}
var alDisabledPrograms = roles.Descendants(...)
...
Or you could maybe use the conditional operator to construct the right predicate:
或者您可以使用条件运算符来构造正确的谓词:
var filter = isDup ? (Func<XElement, bool>)(dp => ...)
: (Func<XElement, bool>)(dp => ...);
var alDisabledPrograms = xlServerRoles.Descendants("ServerRole")
.Where(filter)
.Descendants("ProgramName")
.Select(p => p.Value)
.ToList();
回答by dthorpe
Move the isDup test into the Where expression itself. Use an inline annonymous function instead of a single line expression so that you can use a normal if/else statement.
将 isDup 测试移至 Where 表达式本身。使用内联匿名函数而不是单行表达式,以便您可以使用普通的 if/else 语句。
Like this:
像这样:
var alDisabledPrograms = xlServerRoles.Descendants("ServerRole")
.Where(dp => {
if (isDup)
{
return dp.Element("ServerType").Value == currentColumn.Substring(0, currentColumn.Length - 1))
}
else
{
return dp.Element("ServerType").Value == currentColumn)
})
.Descendants("ProgramName")
.Select(p => p.Value)
.ToList();
回答by Casperah
Insert the isDup in the where clause:
在 where 子句中插入 isDup:
var alDisabledPrograms = xlServerRoles.Descendants("ServerRole")
.Where(dp => isDup ?
(dp.Element("ServerType").Value == currentColumn.Substring(0, currentColumn.Length - 1)) :
(dp.Element("ServerType").Value == currentColumn))
.Descendants("ProgramName")
.Select(p => p.Value)
.ToList();
I think this will do.
我认为这会做。
回答by Servy
Just do it once before the start of the loop:
只需在循环开始前执行一次:
string serverType = currentColumn;
if(isDup)
serverType = currentColumn.Substring(0, currentColumn.Length - 1);
var alDisabledPrograms = xlServerRoles.Descendants("ServerRole")
.Where(dp => dp.Element("ServerType").Value == serverType )
.Descendants("ProgramName")
.Select(p => p.Value)
.ToList();
回答by outcoldman
You can construct query like this:
您可以像这样构造查询:
var query = xlServerRoles.Descendants("ServerRole");
if(isDup)
{
query = query.Where(dp => dp.Element("ServerType").Value == currentColumn.Substring(0, currentColumn.Length - 1))
}
else
{
query = query.Where(dp => dp.Element("ServerType").Value == currentColumn)
}
var alDisabledPrograms = query.Descendants("ProgramName").Select(p => p.Value).ToList();
回答by Kishore Kumar
Place you isDup inside Where
把你放在里面哪里
.Where(dp => dp.Element("ServerType").Value == (isDup ? currentColumn.Substring(0, currentColumn.Length - 1)
: currentColumn))