MongoDB Learning Notes ~ added sorting and expression trees to the Imongorepository interface for the official drive

Source: Internet
Author: User
Tags sorts

MongoDB's official drive, syntax better, stronger

Previously wrote about Imongorepository warehousing, using norm on MongoDB's drive selection, but there was a problem with $ref reference type navigation, which was not very good for reference type property support. or using generics technology a few years ago instead of using attribute, the official driver did better in this respect, so I used the official driver and implemented a imongorepository, encapsulating some of the implementation details on the bottom, We call this official storage for mongoofficialrepository, hehe, who call you come late, mongorepository already for norm occupied, hehe.

New Imongorepository interface content added 4 methods

        /// <summary>        ///official drive, returns the result set with pagination/// </summary>        /// <param name= "PageIndex" ></param>        /// <param name= "pageSize" ></param>        /// <returns></returns>Pagedresult<tentity> Getmodel (intPageIndex,intpageSize); /// <summary>        ///official driver, returns the result set with conditions and pagination/// </summary>        /// <param name= "expression" ></param>        /// <param name= "PageIndex" ></param>        /// <param name= "pageSize" ></param>        /// <returns></returns>Pagedresult<tentity> Getmodel (expression<func<tentity,BOOL>> expression,intPageIndex,intpageSize); /// <summary>        ///official driver, returns a result set with sorting and paging/// </summary>        /// <param name= "Fields" ></param>        /// <param name= "PageIndex" ></param>        /// <param name= "pageSize" ></param>        /// <returns></returns>Pagedresult<tentity> Getmodel (dictionary<expression<func<tentity,Object>>,BOOL> Fields,intPageIndex,intpageSize); /// <summary>        ///official driver, returns the result set with conditions and sorting and pagination/// </summary>        /// <param name= "expression" ></param>        /// <param name= "Fields" ></param>        /// <param name= "PageIndex" ></param>        /// <param name= "pageSize" ></param>        /// <returns></returns>Pagedresult<tentity> Getmodel (expression<func<tentity,BOOL>> expression, dictionary<expression<func<tentity,Object>>,BOOL> Fields,intPageIndex,intPageSize);

The implementation of the four methods, in order not to let MongoDB more details open, so, more cores can only be realized in the warehousing, hehe

         PublicPagedresult<tentity> Getmodel (intPageIndex,intpageSize) {            returnGetmodel (i =true, PageIndex, pageSize); }         PublicPagedresult<tentity> Getmodel (system.linq.expressions.expression<func<tentity,BOOL>> expression,intPageIndex,intpageSize) {            returnGetmodel (expression,NewDictionary<expression<func<tentity,Object>>,BOOL>(), PageIndex, pageSize); }         PublicPagedresult<tentity>Getmodel (Dictionary<expression<func<tentity,Object>>,BOOL>Fields ,intPageIndex,intpageSize) {            returnGetmodel (i =true, Fields, PageIndex, pageSize); }         PublicPagedresult<tentity>Getmodel (Expression<func<tentity,BOOL>>expression, Dictionary<expression<func<tentity,Object>>,BOOL>Fields ,intPageIndex,intpageSize) {sortdefinition<TEntity> sorts =NewObjectsortdefinition<tentity> (New { }); foreach(varIteminchFields ) {                if(item. Value) Sorts=sorts. Ascending (item.                Key); ElseSorts=sorts. Descending (item.            Key); }            varSkip = (PageIndex-1) *pageSize; varLimit =pageSize; varRecordCount = _table. countasync<tentity> (i =true).            Result; return NewPagedresult<tentity>(RecordCount, (int) (RecordCount + pageSize-1) /pageSize, PageSize, PageIndex, _table. Find (expression). Sort (sorts). Skip (Skip). Limit (limit). Tolistasync ().        Result); }

OK, now you can call it in the business layer, the basic above four methods can meet all your needs, hehe!

Look at the UI layer on its call, this demo does not have a BLL layer, hehe

        //        //GET:/background/         PublicActionResult Index (intSort =0,intpage =1) {Dictionary<expression<func<webmanageusers,Object>>,BOOL> sortlist =NewSystem.collections.generic.dictionary<expression<func<webmanageusers,Object>>,BOOL>(); Sortlist.add (i= = I.loginname, sort = =0); varModel = _webmanageusersrepository.getmodel (Sortlist,page,Ten); returnView (model); }

How to see the above code whether the heart itch, haha, quickly go to develop your own MONGODB warehousing it!

MongoDB Learning Notes ~ added sorting and expression trees to the Imongorepository interface for the official drive

Related Article

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.