ISqlExecutor.SelectLazy<TModel>(ISqlQueryBuilder queryBuilder, params object[] parameters) Method
.NET Standard 2.x | Current Version (1.0.1)
Executes the SQL SELECT statement generated by an ISqlQueryBuilder
object and returns an IEnumerable <TModel>
object that can be used to load the result set lazily.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
public IEnumerable<TModel> SelectLazy<TModel>(ISqlQueryBuilder queryBuilder, params object[] parameters);
Type Parameters
TModel
The datatype of the model object which represents the data row in the result set.
You can also specify DynamicModel to TModel
if you do not want to define a custom model class.
Parameters
queryBuilder
SnapObjects.Data.ISqlQueryBuilder
A SqlQueryBuilder
object which can be used to generate a SQL SELECT statement.
parameters
System.Object[]
(Optional) One or more ParamValue objects which contain the values corresponding to the SQL parameter identifier names.
You can also pass the arguments directly in the order in which they appear in the SQL SELECT statement generated by queryBuilder
without using the ParamValue object.
Returns
System.Collections.Generic.IEnumerable<TModel>
Returns an IEunmerable<TModel>
object which can load the result set lazily.
Remarks
This method uses command behavior of System.Data.CommandBehavior.SequentialAccess
internally, which provides a way for the DataReader
to handle rows that contain columns with large binary values.
It creates each model object and reads data from database to this model object only when the data is asked for. You can use this method to save resources and improve performance by deferring the initialization of expensive objects until they are requested.
You have to open the current connection (DataContext.CurrentConnection
property) explicitly before calling this method and close the connection explicitly after calling this method.
Examples
The following code example demonstrates how to use the SelectLazy<TModel>(ISqlQueryBuilder, params object[])
method to build and execute the SQL statement using SqlQueryBuilder and get the result set lazily.
using SnapObjects.Data;
using System;
namespace Appeon.ApiDoc.ISqlExecutorExamples
{
public class SelectLazyExample
{
private readonly SchoolContext _context;
public SelectLazyExample(SchoolContext dataContext)
{
// Sets the data context
_context = dataContext;
}
public void Example2()
{
// Creates SqlQueryBuilder.
var builder = new SqlQueryBuilder();
// Defines the select list.
string[] columns = { "CourseID", "Title" };
// Defines the SQL statement.
builder.Select(columns)
.From("Course")
.Where("CourseID", SqlBuilder.Parameter<int>("id"));
// Opens the current connection.
_context.CurrentConnection.Open();
// Executes the SQL statement.
// It dosen't get the result set from database.
var result = _context.SqlExecutor.SelectLazy<DynamicModel>(builder, 2030);
// Gets each row of the result set from database one by one.
// It creates each model object and reads data from database to
// this model object only when the data is asked for.
// This method is usually used to handle rows that contain columns
// with large binary values.
foreach (var model in result)
{
Console.WriteLine("Title: {0}.", model.GetValue(1));
}
// Closes the current connection.
_context.CurrentConnection.Close();
/*This code produces the following output:
Title: Poetry.
*/
}
}
}
Applies to
.NET Standard
2.x