ISqlModelMapper.Scalar<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 criteria specified in a TModel
class.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
public TValue Scalar<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
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 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 Appeon.ApiDoc.Models.School;
using System;
namespace Appeon.ApiDoc.ISqlModelMapperExamples
{
public class ScalarExample
{
private SchoolContext _context;
public ScalarExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example()
{
// Calculates the minimum budget for departments.
decimal MinBudget = _context.SqlModelMapper
.Scalar<Department, decimal>("min(Budget)");
Console.WriteLine("The minimum budget is {0}.",
MinBudget.ToString("N0"));
/* The code produces the following output:
The minimum budget is 120,000.
*/
}
}
}
Example Refer To
Model Class: Department
Applies to
.NET Standard
2.x