LINQ的if / else条件? [英] Linq if/else condition?

查看:934
本文介绍了LINQ的if / else条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能将是一个新手的问​​题。有没有一种方法来选择取决于布尔值不同的搜索条件?后来在代码中,我通过对象(alDisabledPrograms)要循环。我知道的if / else是不正确的,我把在那里显示我怎么想,要进行处理。我试图把这个值越大越/ else条件内,但无法遍历alDisabledPrograms更高版本。思考?

  VAR alDisabledPrograms = xlServerRoles.Descendants(SERVERROLE)
如果(isDup)
{
。凡(DP => dp.Element(服务器类型)值== currentColumn.Substring(0,currentColumn.Length - 1))
}
,否则
$ { b $ b。凡(DP =方式> dp.Element(服务器类型)值== currentColumn)
}
.Descendants(ProgramName中)
。选择(p值=> ; p.Value)
.ToList();


解决方案

通过您的特定代码,答案其实很简单:

 字符串targetColumn位置= isDup? currentColumn.Substring(0,currentColumn.Length  -  1)
:currentColumn;
VAR alDisabledPrograms = xlServerRoles.Descendants(SERVERROLE)
。凡(DP =方式> dp.Element(服务器类型)值== targetColumn位置)
.Descendants(ProgramName中 )
。选择(p => p.Value)
.ToList();

在一般虽然,可以应用非常不同的查询,您既可以使用:

 的IEnumerable<&的XElement GT;角色= xlServerRoles.Descendants(SERVERROLE); 
如果(isDup)
{
角色= roles.Where(DP => ...);
}
,否则
{
角色= roles.Where(DP => ...);
}
VAR alDisabledPrograms = roles.Descendants(...)
...

或者,你也许可以使用条件运算符来构造正确的谓词:

  VAR过滤= isDup? (Func键<的XElement,布尔>)(DP => ...)
:(Func键<的XElement,布尔>)(DP => ...);
VAR alDisabledPrograms = xlServerRoles.Descendants(SERVERROLE)
。凡(过滤器)
.Descendants(ProgramName中)
。选择(P => p.Value)
.ToList();


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?

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();

解决方案

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();

这篇关于LINQ的if / else条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆