Skip to content

yozian/Yozian.Extension

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Useful Extension methods

You could take a look into test project to get examples with following list!

Extension methods of Type

Take IQueryable Extension method as example

conditional query where closure

The example shows the extension method WhereWhen eliminate if statement block.

The constrain applied only when the condition matched, so that you could achieve dynamic query easily!

var books = await this.dbContext.Book
                .Where(x => x.Name.Equals(request.Name))
                .WhereWhen(
                    !string.IsNullOrEmpty(request.Category),
                    x => x.Category.Equals(request.Category)
                )
                .WhereWhen(
                    request.PublishDate.HasValue,
                    x => x.PublishDate.Equals(request.PublishDate)
                )
                .ToListAsync();

for old school way, would have lots of if statement block

var query = this.dbContext.Book
                .Where(x => x.Name.Equals(request.Name));

if(!string.IsNullOrEmpty(request.Category)){
    query = query.Where(x => x.Category.Equals(request.Category))
}

if(request.PublishDate.HasValue){
    query = query.Where(x => x.PublishDate.Equals(request.PublishDate))
}

var books = await query.ToListAsync();

IQueryable Pagination

  • An Extension method for IQueryable interface
  • Could apply on EntityFramework DbSet query (Any implementation of IQueryable)
        var count = 10;
        var size = 3;
        var source = Enumerable
            .Range(1, count)
            .AsQueryable();

        Pageable<int> result = source.ToPagination(1, size);

        // Async 
        Pageable<int> result = await source.ToPaginationAsync(1, size);

Apply converter

        var count = 10;
        var size = 3;
        var source = Enumerable
            .Range(1, count)
            .AsQueryable();

        Pageable<int, string> result = source.ToPagination(1, size, x => x.ToString());

        // Async 
        Pageable<int, string> result = await source.ToPaginationAsync(1, size, x => x.ToString());

Fetch next page for the same queryable source

        var count = 10;
        var size = 3;
        var source = Enumerable
            .Range(1, count)
            .AsQueryable();

        Pageable<int, string> result = source.ToPagination(1, size, x => x.ToString());

        do
        {
            // process records here
            result.Records.ForEach(it =>
            {
                // do somthing

            });

            result.FetchNextPage();

        }
        while (result.HasNextPage);
      

Fetch next page for the same queryable source

        var count = 32;
        var size = 3;
        var source = Enumerable
            .Range(1, count)
            .AsQueryable();
        var currentPage = 3;

        var result = source.ToPagination(currentPage, size);

        var pageSize = 5;

        // converte to Page<T>, including navigation info.
        Page<int> page = result.ToPage(pageSize);
    
        // also provide tranform method to map records
        Page<string> page = result.ToPage(pageSize, x => x.ToString());


        // dump Page<int> 
        {
          "TotalCount": 32,
          "PageCount": 11,
          "CurrentPage": 3,
          "Size": 3,
          "Records": [
            7,
            8,
            9
          ],
          "PageSize": 5,
          "HasPreviosPages": false,
          "HasNextPages": true,
          "PreviosLastPageNo": 1,
          "NextStartPageNo": 6,
          "NavigationPages": [
            1,
            2,
            3,
            4,
            5
          ]
        }      

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published