Somedeveloper
Somedeveloper

Reputation: 867

Lambda Expression on boolean

What is the correct syntax for lambda expression for a bool value comparrison?

The example below shows rsp.InputOutput which is a bool value. However I get a compiler error when I try to do this. I know its something small, any help appreciated.

In this examplem I want to select all rulesetparameters which have an InputOutput value of true.

validRuleSetParameters.SelectMany(rsp => rsp.InputOutput == true)

thanks Niall

Upvotes: 0

Views: 7000

Answers (3)

Ani
Ani

Reputation: 113402

I think you're just looking for the Where clause:

var ruleSetPars = validRuleSetParameters.Where(rsp => rsp.InputOutput);

SelectMany is quite different; it is used when you want to project each member of a sequence to another sequence, and then flatten the resulting sequence-of-sequences into a single sequence.

Do note that if InputOutput is a boolean property, rsp.InputOutput is already a boolean-expression. Consequently, using the equality operator to produce another boolean expression (by comparing its value with the literal bool true) is unnecessary.

Upvotes: 5

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 120917

validRuleSetParameters.Where(rsp => rsp.InputOutput);

Upvotes: 2

Jackson Pope
Jackson Pope

Reputation: 14640

You want:

validRuleSetParameters.Where(rsp => rsp.InputOutput)

Edit: Where will find all the entries for which the lambda is true. SelectMany is used for flattening a sequence of IEnumerables. Edit 2: Removed == true

Upvotes: 2

Related Questions