Prefetch paths
To specify prefetch paths, an EntityQuery<T> has the method WithPath(prefetchPath). This method accepts one or more prefetch path element instances which can be created with the generated static properties in the generated code (see Adapter or SelfServicing)
Prefetch path elements have the following extension methods to configure them:
- WithSubPath(prefetchPathElement[, prefetchPathElement]…). This method specifies the subpath of the prefetch path element and accepts one or more prefetch path elements which are seen as subpath elements of the prefetch path element the method is called on.
- WithFilter(IPredicate). This method specifies the additional filter used for the prefetch path element
- WithOrdering(sortClause[, sortClause). This method specifies the sort clauses to sort the elements in the specified order.
- WithLimit(int). This method specifies the MaxNumberOfElementsToRetrieve or row limit of the set of elements to fetch.
- WithExclusions(field[, field]..).This method specifies the fields to exclude for the prefetch path element. It overwrites any set of fields already set to exclude/include for the prefetch path element
- WithInclusions(field[, field]..).This method specifies the fields to include for the prefetch path element. It overwrites any set of fields already set to exclude/include for the prefetch path element
- WithJoins(from clause). This method is equal to QuerySpec.From**(), where it allows one to specify a series of joins with the prefetch path node to be able to filter/sort on a related entity. The specified from clause likely always starts with QueryTarget. QueryTarget is in this case converted to the type of the prefetch path element's entity. Aliasing the prefetch path node isn't possible so using QueryTarget.As/TargetAs() is not recommended.
- WithNoCaching(). Used to specify that the prefetch path node's resultset should be excluded from resultset caching.
- WithHint(hint). hint is a string, e.g.
"NOLOCK"
and is the actual hint to apply. -
ForSystemDate(TemporalPredicate[, param1[, param2]]). TemporalPredicate is a string, which is the predicate to emit, e.g.
"AS OF"
. param1 and param2 are optional parameters of typeDateTime
, to be used with the predicate. You can specify just one temporal table predicate per target. Specifying more than one will make the last one be used. - ForBusinessDate(TemporalPredicate[, param1[, param2]]). Equal to ForSystemDate, but now it uses a BusinessDate directive, instead of a SystemDate directive. ForBusinessDate is supported only on DB2 v10+.
Example
var qf = new QueryFactory();
var q = qf.Customer
.Where(CustomerFields.CustomerId.NotIn(new string[] { "FISSA", "PARIS" }))
.WithPath(CustomerEntity.PrefetchPathOrders
.WithFilter(OrderFields.EmployeeId.Equal(2))
.WithOrdering(OrderFields.OrderDate.Ascending())
.ForSystemDate("AS OF {0}", DateTime.Now)
.WithHint("NOLOCK")
.WithLimit(3));
The above query fetches all customers which don't have the values "FISSA" or "PARIS" as CustomerId and with that fetches their first three related Order entities which have for EmployeeId the value 2. The first three are fetched, only as the prefetch path node for Order has a limit defined on itself. It will sort the fetched orders by OrderDate ascending. It will use a temporal predicate "AS OF current date" and a target hint of "NOLOCK".
Please also see the shipped example queries for various usages of the methods mentioned above.