Linq if/else condition?

老子叫甜甜 提交于 2019-12-19 09:02:37

问题


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

回答1:


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



回答2:


Insert the isDup in the where clause:

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.




回答3:


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.

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



回答4:


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



回答5:


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



回答6:


Place you isDup inside Where

.Where(dp => dp.Element("ServerType").Value == (isDup ? currentColumn.Substring(0, currentColumn.Length - 1) 
                                                  : currentColumn))


来源:https://stackoverflow.com/questions/15909926/linq-if-else-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!