コード例 #1
0
        internal static SelectExpression WhereAndSelectDatabaseExpressions(AdventureWorks adventureWorks)
        {
            QueryCompilationContext compilationContext = adventureWorks.GetService <IQueryCompilationContextFactory>()
                                                         .Create(async: false);
            SelectExpression databaseExpression = new SelectExpression(
                dependencies: new SelectExpressionDependencies(adventureWorks.GetService <IQuerySqlGeneratorFactory>(), adventureWorks.GetService <IRelationalTypeMappingSource>()),
                queryCompilationContext: (RelationalQueryCompilationContext)compilationContext);
            MainFromClause querySource = new MainFromClause(
                itemName: "product",
                itemType: typeof(Product),
                fromExpression: Expression.Constant(adventureWorks.ProductCategories));
            TableExpression tableExpression = new TableExpression(
                table: nameof(Product),
                schema: AdventureWorks.Production,
                alias: querySource.ItemName,
                querySource: querySource);

            databaseExpression.AddTable(tableExpression);
            IEntityType      productEntityType = adventureWorks.Model.FindEntityType(typeof(Product));
            IProperty        nameProperty      = productEntityType.FindProperty(nameof(Product.Name));
            ColumnExpression nameColumn        = new ColumnExpression(
                name: nameof(Product.Name), property: nameProperty, tableExpression: tableExpression);

            databaseExpression.AddToProjection(nameColumn);
            databaseExpression.AddToPredicate(Expression.GreaterThan(
                                                  left: new ExplicitCastExpression(
                                                      operand: new SqlFunctionExpression(
                                                          functionName: "LEN",
                                                          returnType: typeof(int),
                                                          arguments: new Expression[] { nameColumn }),
                                                      type: typeof(int)),
                                                  right: Expression.Constant(10)));
            return(databaseExpression.WriteLine());
        }
コード例 #2
0
        internal static SelectExpression SelectAndFirstDatabaseExpressions(AdventureWorks adventureWorks)
        {
            QueryCompilationContext compilationContext = adventureWorks.GetService <IQueryCompilationContextFactory>()
                                                         .Create(async: false);
            SelectExpression selectExpression = new SelectExpression(
                dependencies: new SelectExpressionDependencies(adventureWorks.GetService <IQuerySqlGeneratorFactory>(), adventureWorks.GetService <IRelationalTypeMappingSource>()),
                queryCompilationContext: (RelationalQueryCompilationContext)compilationContext);
            MainFromClause querySource = new MainFromClause(
                itemName: "product",
                itemType: typeof(Product),
                fromExpression: Expression.Constant(adventureWorks.ProductCategories));
            TableExpression tableExpression = new TableExpression(
                table: nameof(Product),
                schema: AdventureWorks.Production,
                alias: querySource.ItemName,
                querySource: querySource);

            selectExpression.AddTable(tableExpression);
            IEntityType productEntityType = adventureWorks.Model.FindEntityType(typeof(Product));
            IProperty   nameProperty      = productEntityType.FindProperty(nameof(Product.Name));

            selectExpression.AddToProjection(new ColumnExpression(
                                                 name: nameof(Product.Name), property: nameProperty, tableExpression: tableExpression));
            selectExpression.Limit = Expression.Constant(1);
            return(selectExpression.WriteLine());
        }