DistinctAttribute Class
.NET Standard 2.x
Specifies that a model class should remove the duplicate values when loading data.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Inherited Constructors
System.Attribute
Syntax
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class DistinctAttribute : Attribute
Constructors
Name | Description |
---|---|
DistinctAttribute() | Initializes a new instance of the DistinctAttribute class. |
Remarks
The Distinct
attribute is the equivalent to adding DISTINCT
to the SQL SELECT statement. It removes duplicate values.
Examples
The following code example demonstrates how to use the Distinct
attribute. It applies the Distinct
attribute to the PersonDistinct class to add DISTINCT to the SQL SELECT statement.
Model: PersonDistinct
using SnapObjects.Data;
using System;
using System.ComponentModel.DataAnnotations.Schema;
namespace Appeon.ApiDoc.Models.School
{
/// <summary>
/// This model class maps to the dbo.Person table.
/// The Distinct attribute will filter the duplicate records when doing the data query.
/// </summary>
[Table("Person", Schema = "dbo")]
[Distinct]
public class PersonDistinct
{
public string LastName { get; set; }
}
}
Example Method:
using SnapObjects.Data;
using Appeon.ApiDoc.Models.School;
using System;
namespace Appeon.ApiDoc.DistinctAttributeExamples
{
public class DistinctAttributeExample
{
private SchoolContext _context;
public DistinctAttributeExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example()
{
// Gets the SQL statement.
string sql = ModelSqlBuilder.GetBuilder<PersonDistinct>(_context).QuerySql;
var names = _context.SqlExecutor.Select<PersonDistinct>(sql);
Console.WriteLine("SQL statement:");
Console.WriteLine(sql);
/* This code example produces the following output:
SQL statement:
SELECT
DISTINCT [LastName]
FROM [dbo].[Person]
*/
}
}
}
Applies to
.NET Standard
2.x