Show / Hide Table of Contents

    IDataIncludeSetter.Include<TKey>(string name, Func<TModel, TKey> property) Method

    .NET Standard 2.x

    Adds the data of an embedded property of TModel object to the IDataPacker object, and specifies a name for the data.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

    IDataIncludeSetter<TModel> Include<TKey>(string name, Func<TModel, TKey> property);
    

    Type Parameter

    TKey

    The type of the embedded property in the TModel object to be included.

    Parameters

    name System.String

    The name specified for the data.

    property System.Func<in T, out TResult>>

    A function used to specify the embedded property in the TModel object to be included.

    Returns

    SnapObjects.Data.IDataIncludeSetter

    Returns the current IDataIncludeSetter object.

    Remarks

    If you use IDataPacker.AddModel to add the data of a TModel object to the IDataPacker object and name it Key1, and then use the IDataIncludeSetter.Include method to add data of an embedded property of the TModel object to the IDataPacker object and name it Key2, then the name will be combined to Key1.Key2.

    If you use IDataPacker.AddModels to add the data of a collection of TModel objects to the IDataPacker object and name it Key1, and then use the IDataIncludeSetter.Include method to add data of an embedded property of the TModel object to the IDataPacker object and name it Key2, then the name will be combined to Key1[n].Key2. n is the zero-based index of a TModel object in its collection.

    Examples

    The following code example packs the TModel object and the embedded data.

    using Appeon.ApiDoc.Models.School;
    using SnapObjects.Data;
    using System;
    using System.Collections.Generic;
    
    namespace Appeon.ApiDoc.IDataIncludeSetterExamples
    {
        public class IncludeExample
        {
            private readonly SchoolContext _context;
    
            public IncludeExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example()
            {
                var packer = new DataPacker();
    
                var mapper = new SqlModelMapper(_context);
    
                var items = mapper.Load<CourseInfo>(2).IncludeAll().ToList();
    
                // Adds the items object and the embedded data to packer.
                string key = "Models";
                packer.AddModels(key, items).Include("Students", m => m.Students);
    
                // Gets and shows the total number of records in packer
                Console.WriteLine("The count is: {0}",
                    packer.Count.ToString());
    
                // Gets and shows all of the key values of packer
                string keys = "";
                for (int i = 0; i < packer.Count; i++)
                {
                    keys = keys + packer.Keys[i] + ", ";
                }
                Console.WriteLine("The key values are: {0}", keys);
    
                /* This code example produces the following output:
                
                The count is: 4
                The key values are: Models, Models[0].Students, Models[1].Students, 
                Models[2].Students,
                */
            }
        }
    }
    

    Example Refer To

    Model Class: CourseInfo

    Applies to

    .NET Standard

    2.x

    See Also

    IDataPacker.AddModel

    IDataPacker.AddModels

    Back to top Generated by Appeon