ISqlModelMapper.Load<TModel>(ISqlQueryBuilder queryBuilder, params object[] parameters) Method
.NET Standard 2.x
Retrieves data according to the SQL query defined in an ISqlQueryBuilder
object.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
public ILoadable<TModel> Load<TModel>(ISqlQueryBuilder queryBuilder, params object[] parameters);
Type Parameters
TModel
The type of a model class.
Parameters
queryBuilder
SnapObjects.Data.ISqlQueryBuilder
An ISqlQueryBuilder
object which can be used to overwrite the static SQL query defined in a TModel
class, so as to perform dynamic retrieval.
parameters
System.Object[]
(Optional) One or more values that you want to use as retrieval arguments in the SQL SELECT statement defined in queryBuilder
.
Returns
SnapObjects.Data.ILoadable<TModel>
Returns the ILoadable<TModel>
object whose methods can be used to further obtain the result set, or perform data operations such as embedded queries.
Examples
The following code example demonstrates how to load departments by a SQL query builder.
using SnapObjects.Data;
using Appeon.ApiDoc.Models.School;
using System;
using System.Collections.Generic;
namespace Appeon.ApiDoc.ISqlModelMapperExamples
{
public class LoadExample
{
private SchoolContext _context;
public LoadExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example2()
{
// Gets SQL query builder from the model class of department.
ISqlQueryBuilder queryBuilder =
ModelSqlBuilder.GetBuilder<Department>(_context).QueryBuilder;
Console.WriteLine("Original SQL is:");
Console.WriteLine(queryBuilder.ToSqlString(_context));
// Adds a SQL WHERE clause dynamically.
queryBuilder.Where("Name", SqlBinaryOperator.Equals, "'Engineering'");
Console.WriteLine();
Console.WriteLine("Current SQL is:");
Console.WriteLine(queryBuilder.ToSqlString(_context));
var engineering = _context.SqlModelMapper
.Load<Department>(queryBuilder)
.FirstOrDefault();
Console.WriteLine();
Console.WriteLine("The department of Engineering:");
Console.WriteLine("DepartmentID is {0}", engineering.DepartmentID);
Console.WriteLine("Name is {0}", engineering.Name);
Console.WriteLine("Budget is {0}", engineering.Budget.ToString("N0"));
/* This code produces the following output:
Original SQL is:
SELECT
[DepartmentID],
[Name],
[Budget],
[StartDate],
[Administrator]
FROM[dbo].[Department]
Current SQL is:
SELECT
[DepartmentID],
[Name],
[Budget],
[StartDate],
[Administrator]
FROM[dbo].[Department]
WHERE([Name] = 'Engineering')
The department of Engineering:
DepartmentID is 1
Name is Engineering
Budget is 350,000
*/
}
}
}
Example Refer To
Model Class: Department
Applies to
.NET Standard
2.x