IDataPacker.AddModels<TModel>([NotNull]this IDataPacker packer, [NotNull]string key, [NotNull]IEnumerable<TModel> models) Method
.NET Standard 2.x
Adds an IEnumerable<TModel>
object as an element to the IDataPacker
object.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
public static IDataIncludeSetter<TModel> AddModels<TModel>(
[NotNull]this IDataPacker packer,
[NotNull]string key,
[NotNull]IEnumerable<TModel> models) where TModel : class
Parameters
key
System.String
The key of the element, which is the identifier of the element in the IDataPacker
object.
value
System.Collections.Generic.IEnumerable<TModel>
The IEnumerable<TModel>
object, which is the value of the element to be added to the IDataPacker
object.
Examples
The following code example adds a list of model objects to the IDataPacker
object and gets the number of elements in IDataPacker
.
using Appeon.ApiDoc.Models.School;
using SnapObjects.Data;
using System;
using System.Collections.Generic;
using System.Data;
namespace Appeon.ApiDoc.IDataPackerExamples
{
public class AddModelsExample
{
private readonly SchoolContext _context;
public AddModelsExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example()
{
var packer = new DataPacker();
List<Course> items = new List<Course>();
string[] titles = { "Chiness", "English" };
// Inserts two model objects to the "items" list
for (int i = 0; i < 2; i++)
{
items.Add(new Course()
{
CourseID = 1000 + i,
Title = titles[i],
Credits = 2,
DepartmentID = 1
});
}
// Adds the "items" list to IDataPacker
packer.AddModels("Models", items);
// Gets and shows the number of elements of IDataPacker
Console.WriteLine("The element count: {0}", packer.Count.ToString());
// Gets and shows the data values in JSON format
Console.WriteLine(packer.GetTextString(DataFormat.Json));
/* This code example produces the following output:
The element count: 1
{"Models":[
{"CourseID":1000,"Title":"Chiness","Credits":2,"DepartmentID":1},
{"CourseID":1001,"Title":"English","Credits":2,"DepartmentID":1}]}
*/
}
}
}
Example Refer To
Model Class: Course
Applies to
.NET Standard
2.x