Show / Hide Table of Contents

    IDataStore.FirstOrDefault<TModel>(Predicate<TModel> predicate) Method

    .NET Standard 2.x

    Generic method. Returns the first data row of the DataStore that meets the criteria specified by a Predicate<TModel> object. If no data row is found, returns null.

    Namespace: DWNet.Data

    Assembly: DWNet.Data.dll

    Syntax

      public TModel FirstOrDefault<TModel>(Predicate<TModel> predicate);
    

    Type Parameters

    TModel

    The type of a model class that matches with the current DataObject.

    Parameters

    predicate System.Predicate<TModel>

    A Predicate<TModel> that you want to use as the search criteria.

    Returns

    TModel

    Returns the TModel object which contains the data of the first row that meets the search criteria.

    Returns null if no row is found.

    Examples

    The following code example demonstrates how to find the first record in DataStore that meets the criteria.

    using Appeon.ApiDoc.Models;
    using DWNet.Data;
    using System;
    
    namespace Appeon.ApiDoc.IDataStoreExamples
    {
        public class FirstOrDefaultExample
        {
            private readonly SchoolContext _context;
    
            public FirstOrDefaultExample(SchoolContext dataContext)
            {
                // Sets the data context
                _context = dataContext;
            }
    
            public void Example2()
            {
                // Instantiates a DataStore object with datawindow: d_department.
                var datastore = new DataStore("d_department", _context);
    
                datastore.Retrieve();
    
                // The first record in the Department table is: 
                // departmentid =1, name=Engineering
                // The second record in the Department table is: 
                // departmentid =2, name=English
                // The following code gets the D_Department object whose 
                // departmentid is larger than 1.
                var department =
                    datastore.FirstOrDefault<D_Department>(d => d.Departmentid > 1);
    
                Console.WriteLine("Department ID: {0}; Department Name: {1}",
                                   department.Departmentid,
                                   department.Name);
    
    
                // The department table has no record whose Departmentid is 
                // larger than 100.
                department = datastore.
                    FirstOrDefault<D_Department>(d => d.Departmentid > 100);
                if (department == null)
                {
                    Console.WriteLine("The department is null.");
                }
                else
                {
                    Console.WriteLine("Department ID: {0}; Department Name: {1}",
                                   department.Departmentid,
                                   department.Name);
                }
    
                /*This code produces the following output:
                 
                Department ID: 2; Department Name: English
                The department is null.
                */
            }
        }
    }
    

    Example Refer To

    Model Class: D_Department
    DataWindow File: d_department

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon