Back to Catalog
We know that the grouping GroupBy in LINQ can group one or more fields in a collection and aggregate one of them, and LINQ provides us with a variety of aggregation methods, from Aver,sum,count and so on, and in the Uncle Privilege system, the above several aggregations are not enough, Because we need to do A bitwise aggregation of the permission field, or the bitwise OR operation of it, this is not difficult for students who have learned the basis of computer, bitwise OR, that is, the value is first converted to binary, the number of two operations to seek or, the principle is: There are 1 1, all 0 is 0, This is the school when the teacher taught, hehe.
extension methods for LINQ Microsoft developers are placed under the System.Linq namespace, so it's best to follow this principle when we write our own extensions, which are written in this namespace, which is handy when used, because vs builds a project, it will reference the namespace itself in config, and in terms of development efficiency is It's very high.
Extension methods for some of the collections that Microsoft encapsulates for us
namespacesystem.linq{//Summary://provides a set of static for querying objects that implement system.collections.generic.ienumerable<t> (in Visual//Shared in Basic) method. Public Static classEnumerable { Public StaticTSource aggregate<tsource> ( ThisIenumerable<tsource> source, Func<tsource, TSource, tsource>func); Public StaticTaccumulate Aggregate<tsource, Taccumulate> ( ThisIenumerable<tsource> source, Taccumulate seed, Func<taccumulate, TSource, taccumulate>func); Public StaticTResult Aggregate<tsource, Taccumulate, tresult> ( ThisIenumerable<tsource> source, Taccumulate seed, Func<taccumulate, TSource, taccumulate> Func, Func< Taccumulate, tresult>resultselector); Public Static BOOLAny<tsource> ( ThisIenumerable<tsource>source); Public Static BOOLAny<tsource> ( ThisIenumerable<tsource> Source, Func<tsource,BOOL> predicate);
The uncle followed the gourd to paint the wind
/// <summary> ///Press or perform bitwise operations///Storage Uncle/// </summary> /// <typeparam name= "TSource" ></typeparam> /// <param name= "source" ></param> /// <param name= "selector" ></param> /// <returns></returns> Public Static intBinaryor<tsource> ( ThisIenumerable<tsource> Source, Func<tsource,int>selector) { intresult =0; foreach(varIteminchsource) {Result|=selector (item); } returnresult; }
When used in an implementation, it is the same as the sum method, except that sum is a sum of values, whereas Binaryor is a bitwise OR of a numeric value, which is completely different from the result of the operation, as shown below
It's all 1,2,4,4,2,1 for aggregation.
Result of Sum () : 14
Binaryor () results: 7
For the practical meaning of bitwise operations: It is often used on enumerations of flags (enumeration element values of 2 n power), as in the permissions section of the uncle's framework, and is stored using this enumeration.
Back to Catalog
EF Architecture ~ Adding bit operations aggregation methods for grouping