Click or drag to resize

ObjectQueryTObject Class

Represent LINQ query, which queries the domain objects repository
Inheritance Hierarchy

Namespace:  Aloe.SystemFrameworks.Domain.LINQ
Assembly:  Aloe.SystemFrameworks.Domain (in Aloe.SystemFrameworks.Domain.dll) Version: 20.1.3.5
Syntax
public abstract class ObjectQuery<TObject> : IOrderedQueryable<TObject>, 
	IEnumerable<TObject>, IEnumerable, IOrderedQueryable, IQueryable, IQueryable<TObject>, 
	IQueryProvider
where TObject : DomainObject

Type Parameters

TObject
The domain object which is queried

The ObjectQueryTObject type exposes the following members.

Constructors
Properties
  NameDescription
Public propertyElementType
Gets the type of the element(s) that are returned when the expression tree associated with this instance of IQueryable is executed.
Public propertyExpression
Gets the expression tree that is associated with the instance of IQueryable.
Public propertyProvider
Gets the query provider that is associated with this data source.
Public propertyRepository
The repository, which is queried by this query
Public propertyTransaction
The transaction to which this query is bound
Top
Methods
Extension Methods
  NameDescription
Public Extension MethodPreloadTObject(ILazyProperty)Overloaded.
Preloads the value of the specified lazy property for the objects in the sequence.
(Defined by LinqExtensions.)
Public Extension MethodPreloadTObject, TResult(ExpressionFuncTObject, TResult)Overloaded.
Preloads the full object state for all objects returned by applying the expression to each element in source. This is still a research method, which will be renamed later to .Preload().
(Defined by LinqExtensions.)
Public Extension MethodCode exampleSplitTObject
Splits the input IEnumerable in chunks with the specified size.
(Defined by Splitter.)
Public Extension MethodTopologicalSortTObject
Topological sort based on item dependencies.
(Defined by RepositoryExtensions.)
Top
Remarks
The ObjectQuery is used to form and execute LINQ queries against object repositories. It contains translator, that supports a subset of the full LINQ command set. The following constructs are supported:

SELECT

Only full object selects are supported, no projections. Example:

from cust in transaction.Query<Customer>()
where ...
select cust;

However, projections are supported after enumeration, as in the following example (pay attention to the .ToList() method call):

from cust in Transaction.Query<Customer>().ToList()
select new { cust.Id, cust.Number };

Take()

Take() is equivalent to SQL's TOP clause and limits the rows returned by the query to the specified number. Example:

var query =
  (from cust in transaction.Query<Customer>()
  where ...
  select cust).Take(10);

This will generate SQL similar to:

SELECT TOP 10 * FROM Customers WHERE ...

WHERE

1. Normal comparison expression

from cust in transaction.Query<Customer>()
where cust.CustomerNumber == "A1035"
select cust;

This will generate SQL similar to:

SELECT * FROM Customers WHERE CustomerNumber = "A1035"

2. Boolean comparison

Boolean fields do not need comparison operator. Example:

from cust in transaction.Query<Customer>()
where cust.IsActive
select cust;

This will generate SQL similar to:

SELECT * FROM Customers WHERE IsActive = 1

3. Including NULLs

You can include null values for fields using the following syntax:

from cust in transaction.Query<Customer>()
where (cust.FromDate ?? DateTime.MinDate) < someDate
select cust;

This will generate SQL similar to:

SELECT * FROM Customers WHERE FromDate < someDate OR FromDate IS NULL

4. In

4.1. You can match a field to a list of values using the following syntax:

var guids = new List<int>() { 1, 2, 3 };
var query = 
  from cust in transaction.Query<Customer>()
  where cust.Id.In(guids)
  select cust;

//this will translate to the same as above, but is more unnatural
var query = 
  from cust in transaction.Query<Customer>()
  where guids.Contains(cust.Id)
  select cust;

Both constructs will generate SQL similar to:

SELECT * FROM Customers WHERE CustomerId IN (1,2,3)

4.2. For references, you can also use .In. For example:

//make a list of Customer objects
var customers = new List<Customer>() { 
  transaction.Get<Customer>(Guid.Parse("00f6eb9e-74fe-e111-82b1-00155d001f00")),
  transaction.Get<Customer>(Guid.Parse("e12d956b-2c8d-463a-bb9f-fa8f2dd6d3c8")),
  transaction.Get<Customer>(Guid.Parse("00f6eb9e-74fe-e111-82b1-00155d001f00")),
  };
//use the list to get all invoices of the three customers  
var query = 
  from inv in transaction.Query<Invoice>()
  where inv.Customer.In(customers)
  select cust;

4.3. You can even use another query variable as list provider or directly nest a query

var customers = transaction.Query<Customer>().Take(3);

var query = 
  from inv in transaction.Query<Invoice>()
  where inv.Customer.In(customers)
  select inv;

var query = 
  from inv in transaction.Query<Invoice>()
  where inv.Customer.In(
    transaction.Query<Customer>().Take(3)
    )
  select inv;

5. .Like()

.Like() can be used for pattern based matching similar to SQL's LIKE operator. Example:

var query = 
  from cust in transaction.Query<Customer>()
  where cust.CustomerNumber.Like("%106%")
  select cust;

This will generate SQL similar to:

SELECT * FROM Customers WHERE CustomerNumber LIKE '%106%'

6. Filtering by parent table

You can filter by the attributes of a parent table. Please, keep in mind, that this does not work for ALL references, but only for ownership references. Examples:

//this will return all products in groups, whose name contains 'tes'
var query =
  from p in transaction.Query<Product>()
  where p.ProductGroup.Name.Like("%tes%")
  select p;

//select all lines, belonging to sales orders, whose date is after 2013/1/1
var query2 =
  from l in transaction.Query<SalesOrderLine>()
  where l.SalesOrder.DocumentDate >= new DateTime(2013, 1, 1)
  select l;

//this will throw exception, because ProductType is not the owner of Product
var query3 = 
  from p in transaction.Query<Product>()
  where p.ProductType.Name.Like("%tes%")
  select p;

The first two queries will generate SQL similar to:

SELECT P.*
FROM Products P 
     JOIN ProductGroups PG 
     ON PG.Id = P.ProductGroupId
WHERE PG.Name LIKE '%tes%'

SELECT SOL.*
FROM SalesOrderLines SOL
     JOIN SalesOrders SO
     ON SO.Id = SOL.SalesOrderId
     JOIN Documents D
     ON D.Id = SO.DocumentId
WHERE D.DocumentDate >= '20130101'
See Also