You can generate SQL statements with the help of methods that are support <IQueryable> interface.
Select, Where, OrderBy, GroupBy, Join – these methods are translated to SQL naturally as SELECT, WHERE, GROUP BY, ORDER BY and JOIN clauses accordingly.
The query below retrieves all companies from USA that have orders latest than 1998 year. The results of query are ordered by OrderId.
SELECT t1."CompanyName", t2."OrderID", t2."OrderDate" FROM "Company" t1 INNER JOIN ("Orders" t2 LEFT OUTER JOIN "Company" t3 ON t2."CompanyID" = t3."CompanyID") ON t1."CompanyID" = t3."CompanyID" WHERE (t1."Country" = :p0) AND (t2."OrderDate" >= :p1) ORDER BY t2."OrderID"
Parameter Name | DbType | Value |
---|---|---|
p0 | String | USA |
p1 | DateTime | 01.01.1998 0:00:00 |
Query and ExecuteQuery methods allow you to execute the SQL code directly. The main difference between them is that the Query method returns an object that implements the IQueryable<T> interface and the ExecuteQuery returns an IEnumerable<T>. It is means that you can enhance results of the Query execution.
The ExecuteCommand method does not return any resultset, however it allows to execute any SQL command directly, without any translation.
The query below returns company list ordered by CompanyName.
SELECT t1."CompanyID", t1."CompanyName", t1."PrimaryContact", t1."Web", t1."Email", t1."AddressTitle", t1."Address", t1."City", t1."Region", t1."PostalCode", t1."Country", t1."Phone", t1."Fax" FROM ( SELECT * FROM "Company" ) t1 ORDER BY t1."CompanyName"
The SqlMethods class provides access for a set of the native SQL functions, among them are Like, Coalesce, Between and so on.
The Contains method is close to the SQL Methods. It is translated to the IN method usually. Here is a simple example of this method. We get all products that belong to the "novel" and "poem" categories:
SELECT t1."CategoryID", t1."CategoryName", t1."ParentCategory" FROM db."Product Categories" t1 WHERE t1."CategoryName" IN (:p0,:p1)
Parameter Name | DbType | Value |
---|---|---|
p0 | String | novel |
p1 | String | poem |
The couple of methods from the DataLoadOptions class allows you to use LazyLoading
The LoadWith method is created for loading child entities with the parent one. The SQL is generated using JOIN for performance reasons. In case, there are several levels of nested LoadWith calls the JOIN will be used only for the first-level queries because the performance will suffer greatly in other case. One of the benefits of Devart LINQ to SQL implementation is that unlike Microsoft DataLoadOptions class, Devart code allows to call LoadWith on the separate query level (not only on the Context level).
The AssociateWith method, unlike the previous one, restricts the child collection. It allows to set a condition and one will obtain only the entities that satisfy this condition when quering the child collection.
Of course, you can combine these methods successfully. Here is a simple example where we get all products name of what is started from "A":
SELECT t1."ProductName", t1."Price", t2."CategoryName" FROM "Products" t1 LEFT OUTER JOIN db."Product Categories" t2 ON t1."CategoryID" = t2."CategoryID" WHERE t1."ProductName" LIKE :p0
The SQL above is generated for Oracle database.
Parameter Name | DbType | Value |
---|---|---|
p0 | String | A% |
These examples illustrate usage of LoadWith and AssociateWith methods. Here we receive all products that belong to the "poem" category.
SELECT t1."CategoryID", t1."CategoryName", t1."ParentCategory", t3."ProductID", t3."ProductName", t3."CategoryID" AS "CategoryID1", t3."UnitName", t3."UnitScale", t3."InStock", t3."Price", t3."DiscontinuedPrice", t3."Discontinued" FROM ( SELECT t2."CategoryID", t2."CategoryName", t2."ParentCategory" FROM "Product Categories" t2 WHERE t2."CategoryName" = :p0 ) t1 LEFT OUTER JOIN "Products" t3 ON (t1."CategoryID" = t3."CategoryID") AND (t3."Price" > :p1) ORDER BY t1."CategoryID"
The SQL above is generated for Oracle database.
Parameter Name | DbType | Value |
---|---|---|
p0 | String | poem |
p1 | Double | 10 |