Interface IQuery<T>
Interface, which describe class for executing query.
Instance which implement this interface can be used for creating and executing query for T model.
Inherited Members
Namespace: Kros.KORM.Query
Assembly: Kros.KORM.dll
Syntax
public interface IQuery<T> : IProjectionQuery<T>, IQueryBase<T>, IOrderedQueryable<T>, IQueryable<T>, IEnumerable<T>, IOrderedQueryable, IQueryable, IEnumerable
Type Parameters
Name | Description |
---|---|
T | Type of model class. |
Remarks
When you don't use Select
or From
function, than default values are taken from model.
Examples
You can use standard string sql query for querying data.
var people = database.Query<Person>().Sql(
"SELECT p.Id, FirstName, LastName, PostCode " +
"FROM Person " +
"JOIN Address ON (Person.AddressId = Address.Id) " +
"WHERE Age > @1", 18);
foreach (var person in people)
{
Console.WriteLine(person.FirstName);
}
You can use sql query builder.
var people = database.Query<Person>()
.Select("p.Id", "FirstName", "LastName", "PostCode")
.From("Person JOIN Address ON (Person.AddressId = Address.Id)")
.Where("Age > @1", 18);
foreach (var person in people)
{
Console.WriteLine(person.FirstName);
}
Methods
From(String)
Add FROM part to sql query.
Declaration
IProjectionQuery<T> From(string table)
Parameters
Type | Name | Description |
---|---|---|
System.String | table | Table name or join. |
Returns
Type | Description |
---|---|
IProjectionQuery<T> | Query for enumerable models. |
Remarks
When From
method is not call, query take table by T model type.
Examples
var people = database.Query<Person>().From("Person LEFT JOIN Avatar ON (Person.Id = Avatar.PersonId)");
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | if |
Select(String)
Add select part to sql.
Declaration
IQuery<T> Select(string selectPart)
Parameters
Type | Name | Description |
---|---|---|
System.String | selectPart | The columns for select clausule. (Separate by ,) |
Returns
Type | Description |
---|---|
IQuery<T> | Query for enumerable models. |
Remarks
When Select
method is not call, query take columns by T model.
Examples
var people = database.Query<Person>().Select("Id, FirstName");
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | if |
Select(String[])
Add columns to sql.
Declaration
IQuery<T> Select(params string[] columns)
Parameters
Type | Name | Description |
---|---|---|
System.String[] | columns | The columns for select clausule. |
Returns
Type | Description |
---|---|
IQuery<T> | Query for enumerable models. |
Remarks
When Select method is not call, query take columns by T model.
Examples
var people = database.Query<Person>().Select("Id", "FirstName");
Select<TResult>(Func<T, TResult>)
Add columns to sql
Declaration
IQuery<T> Select<TResult>(Func<T, TResult> selector)
Parameters
Type | Name | Description |
---|---|---|
System.Func<T, TResult> | selector | The selector. |
Returns
Type | Description |
---|---|
IQuery<T> | Query for enumerable models. |
Type Parameters
Name | Description |
---|---|
TResult | The type of the result. |
Remarks
When Select
method is not call, query take columns by T model.
Examples
var people = database.Query<Person>().Select(p => new { p.Id, p.FirstName });
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | if |
Sql(RawSqlString, Object[])
Create query from sql statement.
Declaration
IQueryBase<T> Sql(RawSqlString sql, params object[] args)
Parameters
Type | Name | Description |
---|---|---|
RawSqlString | sql | The SQL for executing in server. |
System.Object[] | args | The arguments. |
Returns
Type | Description |
---|---|
IQueryBase<T> | Query from sql. |
Remarks
Sql must be server specific. Because no translation is provide.
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | if |
Sql(FormattableString)
Create query from sql statement.
Declaration
IQueryBase<T> Sql(FormattableString sql)
Parameters
Type | Name | Description |
---|---|---|
System.FormattableString | sql | The SQL for executing in server. |
Returns
Type | Description |
---|---|
IQueryBase<T> | Query from sql. |
Remarks
Sql must be server specific. Because no translation is provide.
Examples
var id = 15;
var name = "Name";
var items = query.Sql($"SELECT * FROM Table WHERE Id = {id} AND Name = {name}");
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | if |