2018-02-14 00:09:58 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data.Entity;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using PoweredSoft.DynamicLinq.Fluent;
|
|
|
|
|
|
2018-03-06 20:59:17 -05:00
|
|
|
|
namespace PoweredSoft.DynamicLinq.EntityFramework
|
2018-02-14 00:09:58 -05:00
|
|
|
|
{
|
|
|
|
|
public static class DbContextExtensions
|
|
|
|
|
{
|
|
|
|
|
private static readonly MethodInfo QueryMethod = typeof(DbContextExtensions)
|
|
|
|
|
.GetMethods(BindingFlags.Static | BindingFlags.Public)
|
|
|
|
|
.First(t => t.Name == "Query" && t.IsGenericMethod);
|
|
|
|
|
|
2018-03-14 19:17:25 -04:00
|
|
|
|
public static IQueryable Query(this DbContext context, Type pocoType, Action<WhereBuilderBase> callback)
|
2018-02-14 00:09:58 -05:00
|
|
|
|
{
|
|
|
|
|
var method = QueryMethod.MakeGenericMethod(pocoType);
|
|
|
|
|
var invokeResult = method.Invoke(null, new object[] {context, callback});
|
|
|
|
|
var ret = invokeResult as IQueryable;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-14 19:17:25 -04:00
|
|
|
|
public static IQueryable<T> Query<T>(this DbContext context, Action<WhereBuilderBase> callback)
|
2018-02-14 00:09:58 -05:00
|
|
|
|
where T : class
|
|
|
|
|
{
|
|
|
|
|
var query = context.Set<T>().AsQueryable();
|
|
|
|
|
query = query.Query(callback);
|
|
|
|
|
return query;
|
|
|
|
|
}
|
2018-03-07 21:16:15 -05:00
|
|
|
|
|
2018-03-14 19:17:25 -04:00
|
|
|
|
public static IQueryable Where(this DbContext context, Type pocoType, Action<WhereBuilderBase> callback)
|
2018-03-07 21:16:15 -05:00
|
|
|
|
=> context.Query(pocoType, callback);
|
|
|
|
|
|
2018-03-14 19:17:25 -04:00
|
|
|
|
public static IQueryable<T> Where<T>(this DbContext context, Action<WhereBuilderBase> callback)
|
2018-03-07 21:16:15 -05:00
|
|
|
|
where T : class => context.Query<T>(callback);
|
2018-02-14 00:09:58 -05:00
|
|
|
|
}
|
|
|
|
|
}
|