Show / Hide Table of Contents

    ISqlModelMapper.ScalarAsync<TModel, TValue>(string expression, object[] parameters, CancellationToken cancellationToken) 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, object[] parameters, CancellationToken cancellationToken)
    

    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.

    cancellationToken CancellationToken

    (Optional) A cancellation token that can be used by other objects or threads to receive notice of cancellation.

    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). It uses a CancellationTokenSource type parameter to make this operation cancelable.

    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> Example2(CancellationTokenSource cts)
            {
                decimal MinBudget = 0m;
                
                try
                {
                    // Calculates the minimum budget for departments.
                    // If a task has been cancelled, the call to the ISqlModelMapper.LoadAsync 
                    // method throws an OperationCanceledException.
                    MinBudget = await _context.SqlModelMapper
                                                .ScalarAsync<DepartmentByName, decimal>(
                                                    "min(Budget)",
                                                    new object[] { "English" },
                                                    cts.Token);
                                                
                    Console.WriteLine("The minimum budget is {0}.",
                        MinBudget.ToString("N0"));
                }
                catch (OperationCanceledException e)
                {
                    // The operation was cancelled before completion.
                    Console.WriteLine(e.Message);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    cts.Dispose();
                }
                
                return MinBudget;
                
                /* The code produces the following output:
                
                    A task was canceled.
                */
            }
        }
    }
    

    Example Refer To

    Model Classes: DepartmentByName Department

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon