business

Query Expression (JS, C#)

The Query Expression component enables users to write queries using a unified syntax in both JavaScript and C#. This allows users to seamlessly switch between backend (C#) and frontend (JavaScript) development without needing to rewrite the query logic. By maintaining consistency in syntax, developers can easily copy queries from C# and use them directly in JavaScript, and vice versa, ensuring maximum flexibility and reusability.

How to use Query Expression?
  1. Writing a Query in JS:
  2. <script>
            var query = new QueryExpression('Account ');
            query.Columns.AddColumns("Name", "Address", "PhoneNumber");
            var condition= new ConditionExpression(
                ' Account', //entity
                ' Status',//column
                ConditionOperator.EQUAL, //operator
                '1' //value
                ); query.Criteria.AddCondition(condition)
                </script>
  3. Writing a Query in C#:
  4. <script>
        var query = new QueryExpression(viewSql,” Account “);
        query.Columns.AddColumns("Name", "Address", "PhoneNumber");
        var condition= new ConditionExpression(
            ' Account', //entity
            ' Status',//column
            ConditionOperator.EQUAL, //operator
            '1' //value
            );
            query.Criteria.AddCondition(condition)
            </script>
    
  5. Switching Between C# and JavaScript:
    • Users can directly copy and paste the query between C# and JavaScript with minimal changes. This interoperability simplifies development and reduces the learning curve for developers working across both frontend and backend.
    • Example workflow:
      • A query written in C# for server-side operations can be copied and used in JavaScript for client-side filtering or validation.
      • Similarly, a query written in JavaScript for frontend data manipulation can be ported to C# for use in backend processing.
  6. Advanced Query Features:
    • Both JavaScript and C# support advanced query features such as:
      • Joins: Retrieve related entities using LinkEntity.
      • Sorting: Apply sorting to the query results using the OrderBy clause.
      • Filters: Use multiple conditions and filters (AND/OR) to refine the data being retrieved.
  7. Executing Queries:
    • In Js, queries are executed using:
      const viewExpression= query.GenerateView('custom_view'); 
      var result = await executionContext.webApi.retrieveMultipleRecordsWithJoin(entityId, viewExpression);
    • In C#, queries are executed using:
      var queryExpression = await query.ToString(); 
      var result = await _organizationConnector.RetrieveMutlipleWithReferencesAsync(queryExpression, "Account");