Show / Hide Table of Contents

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

    .NET Standard 2.x

    Executes the SQL SELECT statement generated by an ISqlQueryBuilder object and returns the first row in the result set. All other rows are ignored.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      public TModel SelectOne<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

    TModel

    Returns a TModel object which represents the first row of of the result set. Returns null if there are no rows in the result set.

    Examples

    The following code example demonstrates how to build and execute the SQL statement using SqlQueryBuilder and put the first row of the result set to a model object.

    using SnapObjects.Data;
    using System;
    
    namespace Appeon.ApiDoc.ISqlExecutorExamples
    {
        public class SelectOneExample
        {
            private readonly SchoolContext _context;
    
            public SelectOneExample(SchoolContext dataContext)
            {
                // Sets the data context
                _context = dataContext;
            }
    
            public void Example2()
            {
                // Creates SqlQueryBuilder.
                var builder = new SqlQueryBuilder();
    
                // Defines the SQL statement.
                builder.Select("CourseID")
                       .Select("Title")
                       .From("Course");
    
                // Executes the SQL statement and gets the first row of the result set.
                var result = _context.SqlExecutor.SelectOne<DynamicModel>(builder);
    
                Console.WriteLine("Title: {0}.", result.GetValue<String>("Title"));
    
                /*This code produces the following output:
    
                Title: Calculus.
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon