We have started to share our commonly used extensions for C#. For now, dynamic filtering and ordering is available.
Nuget link: https://www.nuget.org/packages/Hepsiburada.Extensions/
PM> Install-Package Hepsiburada.Extensions
Sorting and filtering by property name was a shameful process if the property is ambiguous at runtime.
- 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;
}
}
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));
}
- 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;
}
}
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