EF architecture ~ Add bitwise operation aggregation methods for groups, ef Architecture
Back to directory
We know that the Group groupby in Linq can group one or more fields in the set and aggregate one of the attributes. However, Linq provides multiple aggregation methods, in the uncle permission system, the above aggregation is not enough because we needPerform bitwise aggregation on permission FieldsOr perform bitwise OR operations on it. This is not difficult for students who have learned computer basics. bitwise OR is used to convert values into binary values first, calculate or for two arithmetic operations. The principle is: there is 1, 1, and all 0 is 0, which was taught by the teacher at school.
Microsoft developers put the extension method of Linq in System. under the namespace of Linq, we 'd better follow this principle when writing extensions by ourselves. They are all written under this namespace, which makes it easy to use, because after VS builds a project, the namespace will be referenced by itself in config, which is highly efficient in development.
Some set extension methods encapsulated by Microsoft for us
Namespace System. linq {// Summary: // provides a set of functions for query to implement System. collections. generic. the static (Shared in Visual/Basic) method of the IEnumerable <T> object. Public static class Enumerable {public static TSource Aggregate <TSource> (this IEnumerable <TSource> source, Func <TSource, TSource, TSource> func); public static TAccumulate Aggregate <TSource, TAccumulate> (this IEnumerable <TSource> source, TAccumulate seed, Func <TAccumulate, TSource, TAccumulate> func); public static TResult Aggregate <TSource, TAccumulate, TResult> (this IEnumerable <TSource> source, TAccumulate seed, Func <TAccumulate, TSource, TAccumulate> func, Func <TAccumulate, TResult> resultSelector ); public static bool Any <TSource> (this IEnumerable <TSource> source); public static bool Any <TSource> (this IEnumerable <TSource> source, Func <TSource, bool> predicate );
Uncle painted with the gourd
/// <Summary> /// bitwise operation // Author: warehouse uncle /// </summary> /// <typeparam name = "TSource"> </typeparam> /// <param name = "source"> </param>/ // <param name = "selector"> </param> // <returns> </returns> public static int BinaryOr <TSource> (this IEnumerable <TSource> source, func <TSource, int> selector) {int result = 0; foreach (var item in source) {result | = selector (item) ;}return result ;}
In implementation, it is the same as the sum method, except that sum is the sum of values, while BinaryOr is the bitwise OR of values, the two are completely different in the calculation result, as shown below:
All are 1, 2, 4, 2, 1 for aggregation.
Sum ()Result: 14
BinaryOr ()Result: 7
The actual meaning of bitwise computation: It is often used in the enumeration of Flags (the enumerated element value is the N power of 2), as in the permissions section of uncle's framework, this enumeration is also used for storage.
Back to directory