Add project files.
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PoweredSoft.DynamicLinq
|
||||
{
|
||||
public enum ConditionOperators
|
||||
{
|
||||
Equal,
|
||||
GreaterThan,
|
||||
GreaterThanOrEqual,
|
||||
LessThan,
|
||||
LessThanOrEqual,
|
||||
Contains,
|
||||
StartsWith,
|
||||
EndsWith
|
||||
}
|
||||
|
||||
internal static class Constants
|
||||
{
|
||||
internal static readonly MethodInfo ContainsMethod = typeof(string).GetMethod("Contains");
|
||||
internal static readonly MethodInfo StartsWithMethod = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
|
||||
internal static readonly MethodInfo EndsWithMethod = typeof(string).GetMethod("EndsWith", new Type[] { typeof(string) });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using PoweredSoft.DynamicLinq.Fluent;
|
||||
using PoweredSoft.DynamicLinq.Helpers;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PoweredSoft.DynamicLinq.Extensions
|
||||
{
|
||||
public static class QueryableExtensions
|
||||
{
|
||||
public static IQueryable<T> Where<T>(this IQueryable<T> query, string path, ConditionOperators conditionOperator, object value, bool convertConstantToLeftOperator = true)
|
||||
{
|
||||
query = query.Query(qb => qb.Compare(path, conditionOperator, value, convertConstantToLeftOperator: convertConstantToLeftOperator));
|
||||
return query;
|
||||
}
|
||||
|
||||
public static IQueryable<T> Query<T> (this IQueryable<T> query, Action<QueryBuilder<T>> callback)
|
||||
{
|
||||
var queryBuilder = new QueryBuilder<T>(query);
|
||||
callback(queryBuilder);
|
||||
var ret = queryBuilder.Build();
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
using PoweredSoft.DynamicLinq.Helpers;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PoweredSoft.DynamicLinq.Fluent
|
||||
{
|
||||
public class QueryBuilder<T>
|
||||
{
|
||||
public IQueryable<T> Query { get; set; }
|
||||
|
||||
public Type QueryableType { get; set; }
|
||||
|
||||
public List<QueryFilterPart> Parts { get; protected set; } = new List<QueryFilterPart>();
|
||||
|
||||
public QueryBuilder(IQueryable<T> query)
|
||||
{
|
||||
Query = query;
|
||||
}
|
||||
|
||||
public QueryBuilder<T> Compare(string path, ConditionOperators conditionOperators, object value,
|
||||
bool convertConstantToLeftOperator = true, bool and = true)
|
||||
{
|
||||
Parts.Add(new QueryFilterPart
|
||||
{
|
||||
And = and,
|
||||
ConditionOperator = conditionOperators,
|
||||
Path = path,
|
||||
Value = value,
|
||||
ConvertConstantToLeftOperator = convertConstantToLeftOperator
|
||||
});
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBuilder<T> SubQuery(Action<QueryBuilder<T>> subQuery, bool and = true)
|
||||
{
|
||||
// create query builder for same type.
|
||||
var qb = new QueryBuilder<T>(Query);
|
||||
|
||||
// callback.
|
||||
subQuery(qb);
|
||||
|
||||
// create a query part.
|
||||
var part = new QueryFilterPart();
|
||||
part.And = and;
|
||||
part.Parts = qb.Parts;
|
||||
Parts.Add(part);
|
||||
|
||||
//return self.
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBuilder<T> And(string path, ConditionOperators conditionOperator, object value, bool convertConstantToLeftOperator = true)
|
||||
=> Compare(path, conditionOperator, value, convertConstantToLeftOperator: convertConstantToLeftOperator, and: true);
|
||||
|
||||
public QueryBuilder<T> Or(string path, ConditionOperators conditionOperator, object value, bool convertConstantToLeftOperator = true)
|
||||
=> Compare(path, conditionOperator, value, convertConstantToLeftOperator: convertConstantToLeftOperator, and: false);
|
||||
|
||||
public QueryBuilder<T> And(Action<QueryBuilder<T>> subQuery)
|
||||
=> SubQuery(subQuery, true);
|
||||
|
||||
public QueryBuilder<T> Or(Action<QueryBuilder<T>> subQuery)
|
||||
=> SubQuery(subQuery, false);
|
||||
|
||||
public IQueryable<T> Build()
|
||||
{
|
||||
var parameter = Expression.Parameter(typeof(T), "t");
|
||||
var expression = BuildExpression(parameter, Parts);
|
||||
var lambda = Expression.Lambda<Func<T, bool>>(expression, parameter);
|
||||
var query = Query.Where(lambda);
|
||||
return query;
|
||||
}
|
||||
|
||||
protected Expression BuildExpression(ParameterExpression parameter, List<QueryFilterPart> parts)
|
||||
{
|
||||
Expression ret = null;
|
||||
|
||||
parts.ForEach(part =>
|
||||
{
|
||||
Expression innerExpression;
|
||||
if (part.Parts?.Any() == true)
|
||||
innerExpression = BuildExpression(parameter, part.Parts);
|
||||
else
|
||||
innerExpression = BuildExpression(parameter, part);
|
||||
|
||||
if (ret != null)
|
||||
ret = part.And ? Expression.And(ret, innerExpression) : Expression.Or(ret, innerExpression);
|
||||
else
|
||||
ret = innerExpression;
|
||||
});
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
private Expression BuildExpression(ParameterExpression parameter, QueryFilterPart part)
|
||||
{
|
||||
var member = QueryableHelpers.ResolvePathForExpression(parameter, part.Path);
|
||||
var constant = part.ConvertConstantToLeftOperator ? QueryableHelpers.GetConstantSameAsLeftOperator(member, part.Value) : Expression.Constant(part.Value);
|
||||
var expression = QueryableHelpers.GetConditionExpressionForMember(parameter, member, part.ConditionOperator, constant);
|
||||
return expression;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PoweredSoft.DynamicLinq.Fluent
|
||||
{
|
||||
public class QueryFilterPart
|
||||
{
|
||||
public string Path { get; set; }
|
||||
public ConditionOperators ConditionOperator { get; set; }
|
||||
public object Value { get; set; }
|
||||
public bool And { get; set; }
|
||||
public bool ConvertConstantToLeftOperator { get; set; }
|
||||
|
||||
public List<QueryFilterPart> Parts { get; set; } = new List<QueryFilterPart>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PoweredSoft.DynamicLinq.Helpers
|
||||
{
|
||||
public static class QueryableHelpers
|
||||
{
|
||||
public static Expression GetConditionExpressionForMember(ParameterExpression parameter, Expression member, ConditionOperators conditionOperator, ConstantExpression constant)
|
||||
{
|
||||
if (parameter == null)
|
||||
throw new ArgumentNullException("parameter");
|
||||
|
||||
if (member == null)
|
||||
throw new ArgumentNullException("member");
|
||||
|
||||
if (constant == null)
|
||||
throw new ArgumentNullException("constant");
|
||||
|
||||
Expression ret = null;
|
||||
|
||||
if (conditionOperator == ConditionOperators.Equal)
|
||||
ret = Expression.Equal(member, constant);
|
||||
else if (conditionOperator == ConditionOperators.GreaterThan)
|
||||
ret = Expression.GreaterThan(member, constant);
|
||||
else if (conditionOperator == ConditionOperators.GreaterThanOrEqual)
|
||||
ret = Expression.GreaterThanOrEqual(member, constant);
|
||||
else if (conditionOperator == ConditionOperators.LessThan)
|
||||
ret = Expression.LessThan(member, constant);
|
||||
else if (conditionOperator == ConditionOperators.LessThanOrEqual)
|
||||
ret = Expression.LessThanOrEqual(member, constant);
|
||||
else if (conditionOperator == ConditionOperators.Contains)
|
||||
ret = Expression.Call(member, Constants.ContainsMethod, constant);
|
||||
else if (conditionOperator == ConditionOperators.StartsWith)
|
||||
ret = Expression.Call(member, Constants.StartsWithMethod, constant);
|
||||
else if (conditionOperator == ConditionOperators.EndsWith)
|
||||
ret = Expression.Call(member, Constants.EndsWithMethod, constant);
|
||||
else
|
||||
throw new ArgumentException("conditionOperator", "Must supply a known condition operator");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the right expression for a path supplied.
|
||||
/// </summary>
|
||||
/// <param name="param">Expression.Parameter(typeOfClassOrInterface)</param>
|
||||
/// <param name="path">the path you wish to resolve example Contact.Profile.FirstName</param>
|
||||
/// <returns></returns>
|
||||
public static Expression ResolvePathForExpression(ParameterExpression param, string path)
|
||||
{
|
||||
Expression body = param;
|
||||
foreach (var member in path.Split('.'))
|
||||
{
|
||||
body = Expression.PropertyOrField(body, member);
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
public static ConstantExpression GetConstantSameAsLeftOperator(Expression member, object value)
|
||||
{
|
||||
if (member == null)
|
||||
throw new ArgumentNullException("member");
|
||||
|
||||
if (value == null)
|
||||
return Expression.Constant(null);
|
||||
|
||||
// the types.
|
||||
var valueType = value.GetType();
|
||||
var memberType = member.Type;
|
||||
|
||||
// if match.
|
||||
if (valueType == memberType)
|
||||
return Expression.Constant(value);
|
||||
|
||||
// attempt a conversion.
|
||||
object convertedValue = TypeHelpers.ConvertFrom(memberType, value);
|
||||
return Expression.Constant(convertedValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PoweredSoft.DynamicLinq.Helpers
|
||||
{
|
||||
public static class TypeHelpers
|
||||
{
|
||||
public static object ConvertFrom(Type type, object source)
|
||||
{
|
||||
object ret = null;
|
||||
|
||||
// safe if null.
|
||||
if (source == null)
|
||||
return ret;
|
||||
|
||||
// not nullable type.
|
||||
var notNullableType = Nullable.GetUnderlyingType(type);
|
||||
if (notNullableType == null)
|
||||
{
|
||||
ret = Convert.ChangeType(source, type);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// the ret.
|
||||
ret = Convert.ChangeType(source, notNullableType);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{2ABC5A60-B549-4ECD-BEF4-31CA7BA4EF06}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>PoweredSoft.DynamicLinq</RootNamespace>
|
||||
<AssemblyName>PoweredSoft.DynamicLinq</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Constants.cs" />
|
||||
<Compile Include="Extensions\QueryableExtensions.cs" />
|
||||
<Compile Include="Helpers\QueryableHelpers.cs" />
|
||||
<Compile Include="Helpers\TypeHelpers.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Fluent\QueryBuilder.cs" />
|
||||
<Compile Include="Fluent\QueryFilterPart.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("PoweredSoft.DynamicLinq")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("PoweredSoft.DynamicLinq")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2018")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("2abc5a60-b549-4ecd-bef4-31ca7ba4ef06")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
Reference in New Issue
Block a user