I am looking for a way to write something like this:
if (product.Category.PCATID != 10 && product.Category.PCATID != 11 && product.Category.PCATID != 16) { }
In a shorthand way like below, which does not work of course:
if (product.Category.PCATID != 10 | 11 | 16) { }
So is there shorthand way at all to do something similar ?
Yes - you should use a set:
private static readonly HashSet<int> FooCategoryIds
= new HashSet<int> { 10, 11, 16 };
...
if (!FooCategoryIds.Contains(product.Category.PCATID))
{
}
You can use a list or an array or basically any collection, of course - and for small sets of IDs it won't matter which you use... but I would personally use a HashSet
to show that I really am only interested in the "set-ness", not the ordering.
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