SqlWhereAttribute Class
.NET Standard 2.x
Sets the search condition for the result set when a model class loads the data.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Inheritance Constructor
System.Attribute
Syntax
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class SqlWhereAttribute : Attribute
Constructors
Name | Description |
---|---|
SqlWhereAttribute(string rawCondition) | Initializes a new instance of the SqlWhereAttribute class. |
SqlWhereAttribute(string left, string right) | Initializes a new instance of the SqlWhereAttribute class. |
Properties
Name | Return Type | Description |
---|---|---|
Left | string | Gets the raw SQL expressions on the left of the operator property. |
Operator | SqlBinaryOperator | Gets or sets the operator used in the WHERE clause of the raw SQL. |
RawCondition | string | Gets the raw SQL search condition in the WHERE clause without the keyword WHERE. |
RawConditionDefined | bool | Gets whether the current instance of the SqlWhere attribute is initialized by a raw SQL search condition. |
Right | string | Gets the raw SQL expression on the right of the operator property. |
Remarks
The SqlWhere
attribute is equivalent to adding a WHERE clause (that defines the search condition for the result set) to the SQL SELECT statement of the model class.
Examples
The following code example demonstrates how to use the SqlWhere
attribute. It applies a SqlWhere
attribute to the DepartmentSqlWhere class, to add the WHERE clause to the SQL SELECT statement.
Model: DepartmentSqlWhere
using SnapObjects.Data;
using System;
using System.ComponentModel.DataAnnotations;
namespace Appeon.ApiDoc.Models.School
{
/// <summary>
/// This model class maps to the dbo.Department table.
/// </summary>
[FromTable("Department", Schema = "dbo")]
[SqlWhere("DepartmentID =1")]
public class DepartmentSqlWhere
{
[Key]
public int DepartmentID { get; set; }
public string Name { get; set; }
public decimal Budget { get; set; }
public DateTime StartDate { get; set; }
public int? Administrator { get; set; }
}
}
Example Method:
using SnapObjects.Data;
using Appeon.ApiDoc.Models.School;
using System;
namespace Appeon.ApiDoc.SqlWhereAttributeExamples
{
public class SqlWhereAttributeExample
{
private SchoolContext _context;
public SqlWhereAttributeExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example()
{
// Gets the SQL statement.
string sql = ModelSqlBuilder.GetBuilder<DepartmentSqlWhere>(_context).QuerySql;
Console.WriteLine("SQL statement:");
Console.WriteLine(sql);
// Executes the SQL statement.
var departments = _context.SqlExecutor.Select<DepartmentSqlWhere>(sql);
/* This code example produces the following output:
SQL statement:
SELECT
[DepartmentID],
[Name],
[Budget],
[StartDate],
[Administrator]
FROM [dbo].[Department]
WHERE ([DepartmentID] =1)
*/
}
}
}
Applies to
.NET Standard
2.x