Show / Hide Table of Contents

    ILoadable.FirstOrDefault() Method

    .NET Standard 2.x

    Returns the first data record in the result set. If there is no data in the result set, returns the default value.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      TModel FirstOrDefault();
    

    Returns

    TModel

    Returns a TModel object which represents the first row of the result set loaded, or returns null if no rows loaded.

    Remarks

    Some methods of ISqlModelMapper (e.g.: ISqlModelMapper.Load and ISqlModelMapper.LoadByKey) can return an ILoadable object whose methods can be used to manipulate the data. The ILoadable.FirstOrDefault method can be used to obtain the first data record that is loaded.

    Examples

    The following code example demonstrates how to load the department of Engineering.

    using Appeon.ApiDoc.Models.School;
    using System;
    using System.Collections.Generic;
    
    namespace Appeon.ApiDoc.ILoadableExamples
    {
        public class FirstOrDefaultExample
        {
            private SchoolContext _context;
    
            public FirstOrDefaultExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example()
            {
                var mapper = _context.SqlModelMapper;
    
                // Loads all departments and gets the list.
                List<Department> depts = mapper.Load<Department>().ToList();
    
                Console.WriteLine("The list of departments:");
                foreach (Department dept in depts)
                {
                    Console.WriteLine(dept.Name);
                }
    
                Console.WriteLine();
    
                // Loads the department of Engineering.
                var engineering = mapper.Load<DepartmentByName>("Engineering")
                                        .FirstOrDefault();
    
                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"));
    
                /* The code produces the following output:
                
                The list of departments:
                Engineering
                English
                Economics
                Mathematics
                 
                The department of Engineering:
                Department ID is 1.
                Name is Engineering.
                Budget is 350,000.
                */
            }
        }
    }
    

    Example Refer To

    Model Classes: Department DepartmentByName

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon