ISqlModelMapper.ScalarByKeyAsync<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 primary key in a TModel
class.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
public Task<TValue> ScalarByKeyAsync<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
Task<TValue>
Returns a task that represents the asynchronous operation.
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 asynchronously for the department of Economics.
using System;
using System.Threading;
using System.Threading.Tasks;
using Appeon.ApiDoc.Models.School;
using SnapObjects.Data;
namespace Appeon.ApiDoc.ISqlModelMapperExamples
{
public class ScalarByKeyAsyncExample
{
private readonly SchoolContext _context;
public ScalarByKeyAsyncExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public async Task<decimal> Example1()
{
// Asynchronously gets the budget for the department of Economics according to the key.
decimal budget = await _context.SqlModelMapper
.ScalarByKeyAsync<Department, decimal>("Budget", 4);
Console.WriteLine("The budget of the department of Economics {0}.",
budget.ToString("N0"));
return budget;
/* The code produces the following output:
The budget of the department of Economics is 200,000.
*/
}
}
}
Applies to
.NET Standard
2.x