Linq condition if / else?

I know that this will probably be a question for beginners. Is there a way to select different search criteria depending on the value of bool? Later in the code, I want to skip the object (alDisabledPrograms). I know if / else is wrong, I added this to show how I would like it to be handled. I tried to put this in a larger if / else condition, but later I could not execute the loop through alDisabledPrograms. 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(); 
+4
source share
6 answers

With your specific 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, to apply a variety of queries, you can 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 use the conditional operator to construct the correct 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(); 
+14
source

Insert isDup into 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 it will be done.

+2
source

Move the isDup test to the Where expression. Use the built-in anonymous function instead of expressing a single line so you can use the 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(); 
+1
source

You can build the query as follows:

 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(); 
+1
source

Do this once before starting 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(); 
0
source

Put you inside. Where

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

All Articles