zaria khan
zaria khan

Reputation: 333

How to do compound conditions in lambda 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")) {

    }

Upvotes: 0

Views: 467

Answers (1)

KMoussa
KMoussa

Reputation: 1578

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"))
{
}

Upvotes: 1

Related Questions