EF Architecture ~ Adding bit operations aggregation methods for grouping

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.