Tuesday, March 28, 2017

Running Asp.net Core Web Application and Web API project on Ubuntu

Step 1: Install .Net Core on Ubuntu using this link

Step 2: On Terminal (Cntrl + Alt + T), go to /home/ directory.  Create dir.  i.e.: mkdir coredemo  If you are new to ubuntu:  /home/ directory is place where it is good to store user specific stuff.

Step 3: If you are creating

Asp.net Core web application, run command:  dotnet web

Asp.net web api, run command:  dotnet api


Step 4: dotnet restore

Step 5: dotnet run


For Testing:

  • Asp.net Core Web application, type in browser: http://localhost:5000  
  • Asp.net Web Api application, type in browser: http://localhost:5000/api/values


(Note: It is possible that port might be different for you, if that is the case change accordingly)

Things to note:  Above approach is using kestrel as web server.  In next post, I will explain how to host both web and api on nginx server.

Monday, March 27, 2017

Efficient way of performing filtering, sorting and pagination with MongoDB C#

Problem: I run into issue where pagination was bringing almost all the rows from the collection and then doing pagination.  In other words it is doing in memory pagination which is inefficient.  Recommendation:  In order to do MongoDB pagination: avoid using LINQ Query.

Solution:
With following way you can easily and efficiently do filtering, sorting and pagination on mongoDB collection.  Below code snippet will bring back only records which are required.  Example:  If pageSize is 10 it will only bring back 10 records.  This code is tested on over 12 million records and it is working efficiently.  I am open for any comment/suggestion to make it more robust.

Note:  It uses predicate way of writing lambda expression.

C# code for filtering, sorting and pagination on MongoDB Collection

//Get MongoDB Collection
var collection = database.GetCollection("Member");


//Query the collection with criteria formed as lambda expression.
var filter = Builders
                 .Filter
                 .Where
                   (
                          a => a.City == "Ahmedabad" &&
                          a.DateOfBirth < DateTime.Now.AddYears(-18)
                    );


//Build sort definition based on your need
var sort = new SortDefinitionBuilder()
                       .Descending(x => x.DateOfJoin);


//Apply Pagination and return back list of records.
var resultSet = collection
                     .Find(filter)
                     .Sort(sort)
                     .Skip(pageSize * (currPage - 1))
                     .Limit(pageSize).ToList();


Most Recent Post

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape