zaria khan zaria khan - 1 year ago 65
C# Question

How to do compound conditions in lamda expression

I have following classes:

public class PriceSelectionsResponse
public Prompts Prompts { get; set; }

public class Prompts
public List<Prompt> prompt { get; set; }

public class Prompt
public int code { get; set; }
public object level { get; set; }
public object message { get; set; }
public object details { get; set; }

Now I want to use an if condition where I want to check that if any
is equal to "WARN" AND that prompt object also CONTAINS string "HOPPER" in
. How can I do that?

I tried this way, but cant figure out. How can I achieve this?

if(PriceSelectionsResponseRootObject.Response.PriceSelectionsResponse.Prompts.prompt.Any(p => p.level == "WARN") && PriceSelectionsResponseRootObject.Response.PriceSelectionsResponse.Prompts.prompt.Where(p=>p.message.contains("Hopper")) {


Answer Source

What your last statement is doing is saying if any of the prompts has level == "WARN" and any of the prompts has a message containing HOPPER (not necessarily the same prompt with level = WARNING) then go into the if block. What you want to do is check that the same prompt matches both conditions:

    .Prompts.prompt.Any(p => p.level.ToString() == "WARN" && p.message.ToString().Contains("HOPPER"))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download