Show / Hide Table of Contents

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

    .NET Standard 2.x

    Retrieves data from the first column, in the first row, for the specified SQL expression according to the primary key in a TModel class.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      public TValue ScalarByKey<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 by the primary table and the primary key(s) in TModel.

    Returns

    TValue

    Returns the data from the first column in the first row of the retrieved data. Its data type is determined by the return value of the SQL expression.

    Remarks

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

    Examples

    The following code example demonstrates how to get the budget for the department of Economics.

    using Appeon.ApiDoc.Models.School;
    using System;
    
    namespace Appeon.ApiDoc.ISqlModelMapperExamples
    {
        public class ScalarByKeyExample
        {
            private SchoolContext _context;
    
            public ScalarByKeyExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example()
            {
                // Gets the budget for the department of Economics according to the key.
                decimal budget = _context.SqlModelMapper
                    .ScalarByKey<Department, decimal>("Budget", 4);
    
                Console.WriteLine("The budget of the department of Economics {0}.",
                    budget.ToString("N0"));
    
                /* The code produces the following output:
                
                The budget of the department of Economics is 200,000.
                */
            }
        }
    }
    

    Example Refer To

    Model Class: Department

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon