Is it possible to build up a bit mask based on the result of a linq query; for example:
class MyClass
{
public int Flag{get;set;}
public bool IsSelected {get;set;}
}
myVar = GetlistMyClass();
int myFlag = myVar.Where(a => a.IsSelected).Select(?);
You can aggregate all flags by using the |-operator like this:
int myFlag = myVar.Where(a => a.IsSelected)
.Select(x => x.Flag)
.Aggregate((current, next) => current | next);
Do you mean a bit flag as in a power of two?
Like this:
Func<int, int> pow2 = null;
pow2 = n => n == 0 ? 1 : 2 * pow2(n - 1);
int myFlag = myVar.Reverse().Select((a, n) => a.IsSelected ? pow2(n) : 0).Sum();
Or do yo simply mean this:
int myFlag = myVar.Where(a => a.IsSelected).Any();
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