Show / Hide Table of Contents

    ISqlModelMapper.ScalarAsync<TModel, TValue>(string expression, params object[] parameters) Method

    .NET Standard 2.x

    Asynchronously retrieves data from the first column, in the first row, for the specified SQL expression, according to the criteria specified in a TModel class.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

    public Task<TValue> ScalarAsync<TModel, TValue>(string expression, params object[] parameters)
    

    Type Parameters

    TModel

    The type of a model class.

    TValue

    The type of the value retrieved.

    Parameters

    expression System.String

    A SQL expression string used to get data from the first column in the first row.

    parameters System.Object[]

    (Optional) One or more values that you want to use as retrieval arguments in the SQL SELECT statement defined in TModel.

    Returns

    Task<TValue>

    Returns a task that represents the asynchronous operation.

    Remarks

    At least one data row must be retrieved according to the specified criteria, otherwise exceptions will be thrown.

    Examples

    The following code example demonstrates how to calculate the minimum budget for departments. The Scalar method retrieves data of the first column in the first row for the SQL expression min(Budget).

    using System;
    using System.Threading;
    using System.Threading.Tasks;
    using Appeon.ApiDoc.Models.School;
    using SnapObjects.Data;
    
    namespace Appeon.ApiDoc.ISqlModelMapperExamples
    {
        public class ScalarAsyncExample
        {
            private readonly SchoolContext _context;
            
            public ScalarAsyncExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public async Task<decimal> Example1()
            {
                // Calculates the minimum budget for departments.
                decimal MinBudget = await _context.SqlModelMapper
                                            .ScalarAsync<Department, decimal>(
                                                "min(Budget)", 
                                                new object[]{1});
                                            
                Console.WriteLine("The minimum budget is {0}.",
                    MinBudget.ToString("N0"));
                    
                // Calculates the budget for departments by name.
                MinBudget = await _context.SqlModelMapper
                                                .ScalarAsync<DepartmentByName, decimal>(
                                                    "Budget",
                                                    new object[] { "Economics" });
                                                    
                Console.WriteLine("The minimum budget is {0}.",
                    MinBudget.ToString("N0"));
                    
                return MinBudget;
                
                /* The code produces the following output:
                
                    The minimum budget is 120,000.
                    The minimum budget is 200,000.
                */
            }
        }
    }
    

    Example Refer To

    Model Classes: DepartmentByName Department

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon