ISqlModelMapper.KeyExists<TModel>(params object[] parameters) Method
.NET Standard 2.x
Checks whether data exists when retrieved, according to the primary key defined in a TModel class.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
public bool KeyExists<TModel>(params object[] parameters);
Type Parameters
TModel
The type of a model class.
Parameters
parameters System.Object[]
(Optional) One or more values that you want to use as retrieval arguments in the SQL SELECT statement defined by the main table and its primary key in TModel.
Returns
System.Boolean
Returns whether data exists.
Remarks
The SQL statement that is executed is formulated according to the main table and primary key defined in TModel.
Examples
The following code example demonstrates how to check if the poetry course exists in the online courses.
using Appeon.ApiDoc.Models.School;
using System;
namespace Appeon.ApiDoc.ISqlModelMapperExamples
{
public class KeyExistsExample
{
private SchoolContext _context;
public KeyExistsExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example()
{
var mapper = _context.SqlModelMapper;
// Gets the course ID of the poetry course.
int courseId = mapper.Load<CourseByTitle>("Poetry")
.FirstOrDefault()
.CourseID;
Console.WriteLine("The course ID of the poetry course is {0}.",
courseId);
// Checks if the poetry course exists in the online courses.
bool hasCourse = mapper.KeyExists<OnlineCourse>(courseId);
Console.WriteLine();
Console.WriteLine("The poetry course exists in the online courses: {0}.",
hasCourse);
/* The code produces the following output:
The course ID of the poetry course is 2030.
The poetry course exists in the online courses: true.
*/
}
}
}
Example Refer To
Model Classes: CourseByTitle OnlineCourse
Applies to
.NET Standard
2.x