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)
Here is how you can use
Contains
to achieve what you want:buildingStatus.Select(item => item.GetCharValue()).Contains(v.Status)
this will return a Boolean value.Use
Any()
instead ofContains()
:If I understand correctly, you need to convert the type (char value) that you store in Building list to the type (enum) that you store in buildingStatus list.
(For each status in the Building list//character value//, does the status exists in the buildingStatus list//enum value//)
I'm not sure precisely what you're looking for, but this program:
produces the expected output:
This program compares a string representation of the enum and produces the same output:
I created this extension method to convert one IEnumerable to another, but I'm not sure how efficient it is; it may just create a list behind the scenes.
Then you can change the where clause to:
and skip creating the
List<string>
withConvertAll ()
at the beginning.The Linq extension method Any could work for you...