IDataIncludeGetter.FirstOrDefault() Method
.NET Standard 2.x
Creates a TModel
object which contains the first row of data from the IDataUnpacker object. Returns null
if there is no data.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
TModel FirstOrDefault();
Returns
TModel
A TModel
object containing the data from the IDataUnpacker object. Returns null
if there is no data.
Examples
The following code example gets the value of the Title
property of the first record in the model object.
using Appeon.ApiDoc.Models.School;
using SnapObjects.Data;
using System;
using System.Collections.Generic;
namespace Appeon.ApiDoc.IDataIncludeGetterExamples
{
public class FirstOrDefaultExample
{
private readonly SchoolContext _context;
public FirstOrDefaultExample(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 to the packer object
string key = "Models";
packer.AddModels(key, items);
var json = packer.GetTextString(DataFormat.Json);
var dataUnPacker = new DataUnpacker(json, DataFormat.Json);
// Gets the model object list from dataUnPacker according to the key.
var models = dataUnPacker.GetModels<CourseInfo>(key);
// Gets the first record of models
var model1 = models.FirstOrDefault();
// Gets and shows the Title value of the first record of models
Console.WriteLine("The title name of the first record is: {0}",
model1.Title);
/* This code example produces the following output:
The title name of the first record is: Composition
*/
}
}
}
Example Refer To
Model Class: CourseInfo
Applies to
.NET Standard
2.x