Skip to content
This repository has been archived by the owner on May 18, 2022. It is now read-only.

Dynamic IQueryable extensions created by Hepsiburada developers.

License

Notifications You must be signed in to change notification settings

hepsiburada/Hepsi.Extensions

Repository files navigation

Hepsi.Extensions v0.1

We have started to share our commonly used extensions for C#. For now, dynamic filtering and ordering is available.

Build status

Installation

Nuget link: https://www.nuget.org/packages/Hepsiburada.Extensions/

PM> Install-Package Hepsiburada.Extensions

Usages

Sorting and filtering by property name was a shameful process if the property is ambiguous at runtime.

DynamicOrderBy

  • IQueryable DynamicOrderBy(this IQueryable source, string propertyName, string direction)
  • IQueryable DynamicOrderBy(this IQueryable source, OrderByRequest orderByRequest)

:(

        public IQueryable<DummyDocument> SortDocuments(IQueryable<DummyDocument> documents, string propertyName)
        {
            switch (propertyName.ToLower())
            {
                case "createdat":
                    return documents.OrderBy(d => d.CreatedAt);
                case "count":
                    return documents.OrderBy(d => d.Count);
                case "id":
                    return documents.OrderBy(d => d.Id);
                case "name":
                    return documents.OrderBy(d => d.Name);
                case "complexype.dummyinteger":
                    return documents.OrderBy(d => d.ComplexType.DummyInteger);
                case "complexype.innercomplextype.dummyinteger":
                    return documents.OrderBy(d => d.ComplexType.InnerComplexType.DummyInnerInteger);

                //Can be hundred cases?... :(

                default:
                    //Can't find the property
                    return documents;
            }
        }

Cool Way

        public IQueryable<DummyDocument> SortDocuments(IQueryable<DummyDocument> documents, string propertyName)
        {
            return documents.DynamicOrderBy(propertyName, "ascending");
        }
        
        //Or you can use our OrderByRequest type as an alternative:
        
        public IQueryable<DummyDocument> SortDocuments(IQueryable<DummyDocument> documents, string propertyName)
        {        
            return documents.DynamicOrderBy(new OrderByRequest(propertyName, OrderByDirection.Ascending));
        }

DynamicFilterBy

  • IQueryable DynamicFiltering(this IQueryable source, string propertyName, string value)

:(

        public IQueryable<DummyDocument> FilterDocuments(IQueryable<DummyDocument> documents, string propertyName, string value)
        {
            switch (propertyName.ToLower())
            {
                case "count":
                    return documents.Where(d => d.Count == Int32.Parse(value));
                case "id":
                    return documents.Where(d => d.Id == Int32.Parse(value));
                case "name":
                    return documents.Where(d => d.Name == value);

                //Can be hundred cases?... :(

                default:
                    //Can't find the property
                    return documents;
            }
        }

Cool Way

        public IQueryable<DummyDocument> FilterDocuments(IQueryable<DummyDocument> documents, string propertyName, string value)
        {
            return documents.DynamicFiltering(propertyName, value);
        }

Note: Dynamic Filtering does not support complex types for now.

Thanks. Hepsiburada Software Development Team

About

Dynamic IQueryable extensions created by Hepsiburada developers.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages