Show / Hide Table of Contents

    ISqlExecutor.Select<TModel>(ISqlQueryBuilder queryBuilder, params object[] parameters) Method

    .NET Standard 2.x

    Executes the SQL SELECT statement generated by an ISqlQueryBuilder object and returns an IList<TModel> object which contains the result set.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      public IList<TModel> Select<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.IList<TModel>

    Returns an IList<TModel> object which contains the result set.

    Examples

    The following code example demonstrates how to use the Select<TModel>(ISqlQueryBuilder, params object[] method to build and execute the SQL statement using SqlQueryBuilder and return the result to a list of models.

    using SnapObjects.Data;
    using System;
    using System.Linq;
    
    namespace Appeon.ApiDoc.ISqlExecutorExamples
    {
        public class SelectExample
        {
            private readonly SchoolContext _context;
    
            public SelectExample(SchoolContext dataContext)
            {
                // Sets the data context
                _context = dataContext;
            }
    
            public void Example2()
            {
                // Creates a SqlQueryBuilder.
                var sql = new SqlQueryBuilder();
    
                // Defines the select list.
                string[] columns = { "CourseID", "Title" };
    
                // Builds the SQL statement.
                sql.Select(columns)
                   .From("Course")
                   .Where("CourseID", SqlBuilder.Parameter<int>("id"));
    
                // Executes the SQL statement and puts the query result to a list of models.
                int id = 1061;
                var list = _context.SqlExecutor.Select<DynamicModel>(sql, id);
    
                Console.WriteLine("Records Count = {0}.",
                    list.Count);
    
                Console.WriteLine("CourseID: {0}",
                    list.FirstOrDefault().GetValue<int>("CourseID"));
    
                Console.WriteLine("Title: {0}",
                    list.FirstOrDefault().GetValue<string>("Title"));
    
                /*This code produces the following output:
    
                Records Count = 1.
                CourseID: 1061
                Title: Physics
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon