zaria khan zaria khan - 19 days ago 5
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
prompt
level
is equal to "WARN" AND that prompt object also CONTAINS string "HOPPER" in
message
. 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

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:

if(PriceSelectionsResponseRootObject.Response.PriceSelectionsResponse
    .Prompts.prompt.Any(p => p.level.ToString() == "WARN" && p.message.ToString().Contains("HOPPER"))
{
}
Comments