void CheckFiltersForQuery <T>(MyDataContext db, IQueryable <T> query) { db.IsSoftDeleteFilterEnabled = true; var resultFiltered1 = query.ToArray(); db.IsSoftDeleteFilterEnabled = false; query = Internals.CreateExpressionQueryInstance <T>(db, query.Expression); var resultNotFiltered1 = query.ToArray(); Assert.That(resultFiltered1.Length, Is.LessThan(resultNotFiltered1.Length)); var currentMissCount = Query <T> .CacheMissCount; db.IsSoftDeleteFilterEnabled = true; query = Internals.CreateExpressionQueryInstance <T>(db, query.Expression); var resultFiltered2 = query.ToArray(); db.IsSoftDeleteFilterEnabled = false; query = Internals.CreateExpressionQueryInstance <T>(db, query.Expression); var resultNotFiltered2 = query.ToArray(); Assert.That(resultFiltered2.Length, Is.LessThan(resultNotFiltered2.Length)); AreEqualWithComparer(resultFiltered1, resultFiltered2); AreEqualWithComparer(resultNotFiltered1, resultNotFiltered2); Assert.That(currentMissCount, Is.EqualTo(Query <T> .CacheMissCount), () => "Caching is wrong."); }
/// <summary> /// Creates instance of adapter. /// </summary> /// <param name="dataContext">Data context instance.</param> /// <param name="expression">Query expression.</param> public LinqToDBForEFQueryProvider(IDataContext dataContext, Expression expression) { if (expression == null) { throw new ArgumentNullException(nameof(expression)); } var dataContext1 = dataContext ?? throw new ArgumentNullException(nameof(dataContext)); QueryProvider = (IQueryProviderAsync)Internals.CreateExpressionQueryInstance <T>(dataContext1, expression); QueryProviderAsQueryable = (IQueryable <T>)QueryProvider; }
public void CreatingQuery([IncludeDataSources(TestProvName.AllSQLite)] string context) { using (var db = GetDataContext(context)) using (var table = db.CreateLocalTable <SampleClass>()) { var queryable = table.Where(t => t.Id == 1); var newQueryable = Internals.CreateExpressionQueryInstance <SampleClass>(db, queryable.Expression); newQueryable.ToArray(); } }
static bool InitializeInternal() { var prev = LinqExtensions.ProcessSourceQueryable; InitializeMapping(); var instantiator = MemberHelper.MethodOf(() => Internals.CreateExpressionQueryInstance <int>(null, null)) .GetGenericMethodDefinition(); LinqExtensions.ProcessSourceQueryable = queryable => { // our Provider - nothing to do if (queryable.Provider is IQueryProviderAsync) { return(queryable); } var context = Implementation.GetCurrentContext(queryable); if (context == null) { throw new LinqToDBForEFToolsException("Can not evaluate current context from query"); } var dc = CreateLinqToDbContext(context); var newExpression = queryable.Expression; var result = (IQueryable)instantiator.MakeGenericMethod(queryable.ElementType) .Invoke(null, new object[] { dc, newExpression }); if (prev != null) { result = prev(result); } return(result); }; LinqExtensions.ExtensionsAdapter = new LinqToDBExtensionsAdapter(); // Set linq2db to allow multiple queries by default Common.Configuration.Linq.AllowMultipleQuery = true; return(true); }
async Task <TResult> IQueryProviderAsync.ExecuteAsync <TResult>(Expression expression, CancellationToken token) { var entity = await LinqToDBQuery.ExecuteAsync <TResult>(expression, token); if (entity is T tEntity) { var db = this.GetDataContext <IDataContext>(); var resultingQuery = Internals.CreateExpressionQueryInstance <TResult>(db, expression); var queryToPass = from x in this where x == resultingQuery select x; _rootAccessor.LoadMap(new List <T> { tEntity }, queryToPass, _builder); } return(entity); }