I have a List<BuildingStatus>
called buildingStatus
. I'd like to check whether it contains a status whose char code (returned by GetCharCode()
) equals some variable, v.Status
.
Is there some way of doing this, along the lines of the (non-compiling) code below?
buildingStatus.Contains(item => item.GetCharValue() == v.Status)
All, Any & Contains are quantifier operators in LINQ. All checks if all the elements in a sequence satisfies the specified condition. Any check if any of the elements in a sequence satisfies the specified condition. Contains operator checks whether specified element exists in the collection or not.
The term 'Lambda expression' has derived its name from 'lambda' calculus which in turn is a mathematical notation applied for defining functions. Lambda expressions as a LINQ equation's executable part translate logic in a way at run time so it can pass on to the data source conveniently.
Lambda expressions in C# are used like anonymous functions, with the difference that in Lambda expressions you don't need to specify the type of the value that you input thus making it more flexible to use. The '=>' is the lambda operator which is used in all lambda expressions.
Use Any()
instead of Contains()
:
buildingStatus.Any(item => item.GetCharValue() == v.Status)
The Linq extension method Any could work for you...
buildingStatus.Any(item => item.GetCharValue() == v.Status)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With