Skip to content

Instantly share code, notes, and snippets.

View enisn's full-sized avatar

Enis Necipoglu enisn

View GitHub Profile
@enisn
enisn / ProductsController.cs
Last active April 16, 2020 20:41
Challenge #1 - Solution 1 - Contains Query Manual
var parameter = Expression.Parameter(typeof(Product), "x");
var property = Expression.Property(parameter, "Name")
// Find single parameter 'Contains' method of string with reflection:
var method = typeof(string).GetMethod(nameof(string.Contains), types: new[] { typeof(string) });
var comparison = Expression.Call(
method: method,
instance: property
arguments: new []{ Expression.Constant(filter.Name) });
var lambda = Expression.Lambda<Func<Product, bool>>(comparison, parameter);
@enisn
enisn / Queries.cs
Created April 16, 2020 19:30
Challenge #1 - Solution 1 - Range And - Classic
var query = db.Products.Where(x => x.Stock > 0 && x.Stock < 9);
@enisn
enisn / Queries.cs
Last active April 16, 2020 19:35
Challenge #1 - Solution 1 - Range And - Manual
var parameter = Expression.Parameter(typeof(Product), "x");
var property = Expression.Property(parameter, "Stock");
var comparisonGt = Expression.GreaterThan(property, Expression.Constant(0));
var comparisonLt = Expression.LessThan(property, Expression.Constant(9));
var comparison = Expression.And(comparisonGt, comparisonLt);
var lambda = Expression.Lambda<Func<Product,bool>>(comparison, parameter);
var query = db.Products.Where(lambda);
@enisn
enisn / IFilter.cs
Created April 16, 2020 19:51
Challenge #1 - Solution 2 - IFilter
public interface IFilter
{
Expression BuildExpression(Type entityType, Expression body);
IQueryable<TEntity> ApplyFilterTo<TEntity>(IQueryable<TEntity> query);
}
@enisn
enisn / FilterBase.cs
Created April 16, 2020 19:53
Challenge #1 - Solution 2 - FilterBase.cs
public class FilterBase : IFilter
{
public virtual CombineType CombineWith { get; set; }
public virtual IQueryable<TEntity> ApplyFilterTo<TEntity>(IQueryable<TEntity> query)
{
var parameter = Expression.Parameter(typeof(TEntity), "x");
var exp = BuildExpression(typeof(TEntity), parameter);
if (exp == null)
@enisn
enisn / IFilterableType.cs
Created April 16, 2020 20:00
Challenge # 1 - Solution 1 - IFilterableType.cs
public interface IFilterableType
{
Expression BuildExpression(Expression expressionBody, PropertyInfo property, object value);
}
@enisn
enisn / FilteringOptionsBase.cs
Created April 16, 2020 20:02
Challenge #1 - Solution 2 - FilteringOptionsBase.cs
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public abstract class FilteringOptionsBaseAttribute : Attribute, IFilterableType
{
public abstract Expression BuildExpression(Expression expressionBody, PropertyInfo property, object value);
}
@enisn
enisn / StringFİlterOptionsAttribute.cs
Last active April 16, 2020 20:04
Challenge #1 - Solution 2 - StringFilterOptionsAttribute.cs
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class StringFilterOptionsAttribute : FilteringOptionsBaseAttribute
{
public StringFilterOptionsAttribute(StringFilterOption option)
{
this.Option = option;
}
public StringFilterOption Option { get; set; }
public StringComparison StringComparison { get; set; } = StringComparison.InvariantCultureIgnoreCase;
@enisn
enisn / StringFilterOption.cs
Created April 16, 2020 20:05
Challenge #1 - Solution 2 - StringFilterOption.cs
[Flags]
public enum StringFilterOption
{
Equals = 0,
StartsWith = 1 << 0,
EndsWith = 1 << 1,
Contains = 1 << 2,
}
@enisn
enisn / IRange{T}.cs
Created April 16, 2020 20:11
Challenge #1 - Solution 2 - IRange{T}.cs
public interface IRange<T> : IFilterableType where T :struct, IComparable
{
T? Min { get; set; }
T? Max { get; set; }
}