Show / Hide Table of Contents

    ISqlExecutor.SelectLazy<TModel>(string sqlText, params object[] parameters) Method

    .NET Standard 2.x

    Executes the SQL SELECT statement and returns an ILazyQueryable<TModel> object that can be used to load the result set lazily.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      public ILazyQueryable<TModel> SelectLazy<TModel>(string sqlText, 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

    sqlText System.String

    A raw SQL Statement which contains the parameter placeholders.

    The proper syntax of a parameter placeholder is specific to the data source (e.g., @newFirstName for SQL Server). To make a parameterized SQL ( e.g., update Person set FirstName = @newFirstName where PersonId = 1), these placeholders are filled in with the actual parameter values when the SQL statement is executed.

    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 each parameter identifier name appears in sqlText for the first time, without using the ParamValue object.

    Returns

    SnapObjects.Data.ILazyQueryable<TModel>

    Returns an ILazyQueryable<TModel> object which can load the result set lazily.

    Remarks

    This method uses command behavior of 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.

    After obtaining the required data, you need to call the ILazyQueryable <Model>.Dispose method to release the resources occupied by the database connection and DataReader. Before the returned ILazyQueryable <Model> object is disposed, the current DataContext cannot be used for other data operations. Otherwise, an exception will be thrown.

    However, in some common cases, you can benefit from C# and do not have to explicitly call the ILazyQueryable <Model>.Dispose method, for example, when using the C# using statement or the C# foreach statement, it can implicitly dispose ILazyQueryable <Model>.

    Examples

    The following code example demonstrates how to use the SelectLazy<TModel>(String, params object[]) method to execute the SQL statement lazily.

    using SnapObjects.Data;
    using System;
    using System.Linq;
    
    namespace Appeon.ApiDoc.ISqlExecutorExamples
    {
        public class SelectLazyExample
        {
            private readonly SchoolContext _context;
            
            public SelectLazyExample(SchoolContext dataContext)
            {
                // Sets the data context
                _context = dataContext;
            }
    
            public async void Example1()
            {
                // Defines the SQL statement.
                var sql = @"SELECT CourseID, Title
                            FROM Course
                            WHERE CourseID = @Courseid";
                            
                // Coding style 1:
                // Executes the SQL statement. It doesn't get the result set from database eagerly.
                // This method is usually used to handle rows that contain columns  
                // with large binary values.
                // No need to close the connection explicitly as 'using' can dispose result1 automatically 
                await using (var result1 = _context.SqlExecutor.SelectLazy<DynamicModel>(sql, 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.
                    await foreach (var model in result1)
                    {
                        Console.WriteLine("Result 1: {0}.", model.GetValue<string>(1));
                    }
                }
                
                // Coding style 2:
                // Executes the SQL statement.
                // It doesn't get the result set from database eagerly.
                // This method is usually used to handle rows that contain columns  
                // with large binary values.
                // If result2 hasn't been disposed, you need to dispose result2 explicitly.
                var result2 = _context.SqlExecutor.SelectLazy<DynamicModel>(sql, 1050);
                
                await foreach (var model in result2)
                {
                    Console.WriteLine("Result 1: {0}.", model.GetValue<string>(1));
                }
                //string title = result2.FirstOrDefault().GetValue<string>(1);
                
                //Console.WriteLine("Result 2: {0}.", title);
                
                // Disposes result2 explicitly.
                //result2.Dispose();
                
                
                /*This code produces the following output:
                
                Result 1: Poetry.
                Result 2: Chemistry.
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon