Accepted answer

Isn't it as simple as this condition below? You don't have to check twice since you can directly access IsActive property.

if (AffiliateConfigurations.Any(x => x.Name.Equals("ProviderType") && !x.IsActive))



You are performing two validations: - First you check if there is at least one item in your collection that the name matches "ProvidersType". - The you iterate through your collection again in order to find such item (it will exist since the first verification has passed) and check if it is active.

You can do that in a single run: if (AffiliateConfigurations.Any(x => x.Name == "ProviderType" && x.IsActive == false /* or "!x.IsActive" */)) { //perform some action }

If your "AffiliateConfigurations" is an IEnumerable most of the linq method that can "filter" your collection will accept a Func<TypeBeingFiltered, bool> or a Predicate<TypeBeingFiltered> (like Any, Where, FirstOrDefault, Single, ...), you can give any type of code bock as part of those parameters as long as you return a bool value at the end.


Simply use this condition like #John Woo said

x => x.Name.Equals("ProviderType") && !x.IsActive

which loop through the AffiliateConfigurations for only one time, where in your approach it have to loop through for two time one for Any part and one for FirstOrDefault part...


You can directly check IsActive

if (AffiliateConfigurations.Any(x => x.Name.Equals("ProviderType") && !x.IsActive ) )
    //perform some action

Related Query

More Query from same tag