Define an alias for the models, to use in queries.
var t0 = new Orders().AsQueryable();
"Sql Commands" (such as AS, AVG, DISTINCT etc.) can be used via helper parameter that comes with expression function.
Odb.SQL.Select(x => ... x[table-field].SqlCommand ... );
CONSTANT
Selects a contant value.
Odb.SQL.Select(x =>
x.Fields(
{{hl}}5.60{{/hl}}, {{hl}}"str1"{{/hl}}, {{hl}}x[5.63].AS("C1"){{/hl}}
)
);
AS
Renames a column with an alias.
Odb.SQL.Select(x =>
x.From(t0)
.Fields(
x["X"].{{hl}}AS("C1"){{/hl}}, x[t0.OrderID].{{hl}}AS("OID"){{/hl}}
)
);
IN / NOT IN
IN command allows you to specify multiple values in a WHERE clause.
var vals1 = new[] {1, 2};
var vals2 = new[] {"A", "B", "C"};
Odb.SQL.Select(x =>
x.From(t0)
.Where( x[t0.OrderID].{{hl}}IN(vals1){{/hl}} &
x[t0.EmployeeID].{{hl}}NOT_IN(vals2){{/hl}}
)
);
IS NULL / IS NOT NULL
Odb.SQL.Select(x =>
x.From(t0)
.Where( (object)t0.OrderID {{hl}}== DBNull.Value{{/hl}} &
(object)t0.EmployeeID {{hl}}!= DBNull.Value{{/hl}}
)
);
LIKE
is used in a WHERE clause to search for a specified pattern in a column.
Odb.SQL.Select(x =>
x.From(t0)
.Where( x[t0.Country].{{hl}}STARTS_WITH("A"){{/hl}} |
x[t0.Country].{{hl}}ENDS_WITH("Z"){{/hl}} |
x[t0.Country].{{hl}}CONTAINS("M"){{/hl}}
)
);
DISTINCT
Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.
The DISTINCT statement is used to return only distinct (different) values.
Odb.SQL.Select(x =>
x.From(t0)
.Fields(
{{hl}}x{{/hl}}[t0.Country].{{hl}}DISTINCT(){{/hl}}.AS("C")
)
);
COALESCE
Return the first non-null value in a list.
Odb.SQL.Select(x =>
x.From(t0)
.Fields(
{{hl}}x{{/hl}}[t0.CustomerName].{{hl}}COALESCE('???'){{/hl}}.AS("C")
)
);
Aggregate Functions
Odb.SQL.Select(x =>
x.From(t0)
.{{hl}}GroupBy(t0.Country){{/hl}}
.Fields( th.Country,
{{hl}}x{{/hl}}[t0.CustomerID].{{hl}}AVG(){{/hl}}.AS("C1"),
{{hl}}x{{/hl}}[t0.CustomerID].{{hl}}MIN(){{/hl}}.AS("C2"),
{{hl}}x{{/hl}}[t0.CustomerID].{{hl}}MAX(){{/hl}}.AS("C3"),
{{hl}}x{{/hl}}[t0.CustomerID].{{hl}}SUM(){{/hl}}.AS("C4")
)
);
String Functions
Odb.SQL.Select(x =>
x.From(t0)
.Fields( {{hl}}x{{/hl}}[t0.City].{{hl}}SUBSTRING(2, 4){{/hl}}.AS("2_to_4"),
{{hl}}x{{/hl}}[t0.City].{{hl}}SUBSTRING(0, 1){{/hl}}.AS("left_1"),
{{hl}}x{{/hl}}[t0.City].{{hl}}SUBSTRING(-1, 2){{/hl}}.AS("right_2"),
{{hl}}x{{/hl}}[t0.City].{{hl}}UPPER(){{/hl}}.AS("C1"),
{{hl}}x{{/hl}}[t0.City].{{hl}}LOWER(){{/hl}}.AS("C2"),
{{hl}}x{{/hl}}[t0.City].{{hl}}TRIM(){{/hl}}.AS("C3"),
{{hl}}x{{/hl}}[t0.City].{{hl}}RTRIM().LTRIM(){{/hl}}.AS("C4"),
{{hl}}x{{/hl}}[t0.City].{{hl}}CONCAT(", ID=", t0.CustomerID){{/hl}}.AS("C5")
)
);