Click or drag to resize

IQueryResultSetTData Interface

Represents a query result collection which allows for delay loading of results

Namespace: SanteDB.Core.Model.Query
Assembly: SanteDB.Core.Model (in SanteDB.Core.Model.dll) Version: 3.0.2081-alpha+0a330d5f5a5d3cb3e6e945ef530e94ac5671b49c
Syntax
public interface IQueryResultSet<TData> : IQueryResultSet, 
	IEnumerable, IEnumerable<TData>

Type Parameters

TData

[Missing <typeparam name="TData"/> documentation for "T:SanteDB.Core.Model.Query.IQueryResultSet`1"]

The IQueryResultSetTData type exposes the following members.

Properties
 NameDescription
Public propertyElementType Gets the types of elements which can be filtered and/or manipulated in the collection
(Inherited from IQueryResultSet)
Top
Methods
 NameDescription
Public methodAny Returns true if any results match
(Inherited from IQueryResultSet)
Public methodAsStateful Get the result set as a stateful query
Public methodCount Return only the count of the objects
(Inherited from IQueryResultSet)
Public methodDistinct Get only distinct objects in the collection
Public methodExcept Do not include in the result set any objects which match the query
Public methodFirst Retrieve the first result otherwise throw exception
Public methodFirstOrDefault Retrieve the first result otherwise return default
Public methodGetEnumeratorReturns an enumerator that iterates through a collection.
(Inherited from IEnumerable)
Public methodIntersect(ExpressionFuncTData, Boolean) Intersect this result set with another
Public methodIntersect(IQueryResultSet) Intersect this set and other
(Inherited from IQueryResultSet)
Public methodIntersect(IQueryResultSetTData) Intersect this result set with another
Public methodOfTypeTType Flatten this object model to an IEnumerable of type TType
(Inherited from IQueryResultSet)
Public methodSelectTReturn(Expression) Select a single object from the object
(Inherited from IQueryResultSet)
Public methodSelectTReturn(ExpressionFuncTData, TReturn) Select a single object from the object
Public methodSingle Retrieve one (and only one) result otherwise throw exception
Public methodSingleOrDefault Retrieve one (and only one) otherwise return null if not found or throw if more than one
Public methodSkip Skip count results
Public methodTake Take only count results
Public methodUnion(ExpressionFuncTData, Boolean) Union the results in this set with those matching query
Public methodUnion(IQueryResultSet) Union this set and other
(Inherited from IQueryResultSet)
Public methodUnion(IQueryResultSetTData) Union the results in this set with those in the other
Public methodWhere(Expression) Filter the result set where the specified condition matches
(Inherited from IQueryResultSet)
Public methodWhere(ExpressionFuncTData, Boolean) Filter the result set where the specified condition matches
Top
Extension Methods
 NameDescription
Public Extension MethodApplyResultInstructions Apply result instructions
(Defined by QueryControlUtility)
Public Extension MethodAsResultSet As result set
(Defined by ExtensionMethods)
Public Extension MethodAsResultSetTData As result set
(Defined by ExtensionMethods)
Public Extension MethodForEachTData For each item in an enumerable
(Defined by ExtensionMethods)
Public Extension MethodIsNullOrEmpty Returns true if the IList is null or has no elements
(Defined by ExtensionMethods)
Public Extension MethodOrderByTData, TKey Order result set
(Defined by ExtensionMethods)
Public Extension MethodOrderByDescendingTData, TKey Order result set
(Defined by ExtensionMethods)
Public Extension MethodToDictionaryIgnoringDuplicatesTData, TKey Creates a DictionaryTKey, TValue from an IEnumerableT according to specified key selector function. Diplicate keys will not be added to the dictionary.
(Defined by ExtensionMethods)
Public Extension MethodToDictionaryIgnoringDuplicatesTData, TKey, TElement Creates a DictionaryTKey, TValue from an IEnumerableT according to specified key selector and element selector functions. Diplicate keys will not be added to the dictionary.
(Defined by ExtensionMethods)
Public Extension MethodTransformResultSetTFrom, TTo As a result set
(Defined by ExtensionMethods)
Top
Remarks

Implementers of this interface support yielded or late-executed queries. For example, when calling a Query() method on a persistence class, the query to the database is not executed immediately, rather the first call to a method which iterates over this collection, or calls or otherwise fetches a record results in the query being executed

This is done to reduce the amount of data which is loaded from the database, however it also means that careless use of the result set will result in many additional queries to the database. Consider:

C#
var results = this.persistenceService.Query(o => o.StatusConceptKey == StatusKeys.Active, AuthenticationContext.Current.Principal);
var count = results.Count();
var first = results.First();
var second = results.Skip(1).First();

Would result in 3 calls to the database:

  1. The Count() call results in a SQL SELECT COUNT(*)
  2. The First() method results in a SQL SELECT * .... FETCH FIRST 1 ROWS ONLY call
  3. The Skip(1).First() method results in a SQL SELECT * .... OFFSET 1 FETCH FIRST 1 ROWS ONLY call

If the caller wishes to make multiple calls to this interface, it is recommended that they realize the results into an IEnumerable instance, for example:

var results = this.persistenceService.Query(o => o.StatusConceptKey == StatusKeys.Active, AuthenticationContext.Current.Principal).ToList(); var count = results.Count(); var first = results.First(); var second = results.Skip(1).First();

Results in only one SELECT * FROM xxx method, all results are loaded into memory and then subsequent calls are made in memory.

This may seem more efficient, however imagine a result set where only the second page is required (10 results per page), we want the COUNT of all records however only actually want to load 10 records from the database, in this case we want two SQL queries executed (one for counting and another for fetching)

var results = this.persistenceService.Query(o => o.StatusConceptKey == StatusKeys.Active, AuthenticationContext.Current.Principal); var count = results.Count(); var results = results.Skip(10).Take(10).ToList();

This code executes two SQL queries, a SELECT COUNT(*) FROM xxxx and then another SELECT * FROM ... OFFSET 10 FETCH FIRST 10 ROWS ONLY , this greatly reduces the result set. We can also determine if we want to send queries to the database with Any, which would execute a SELECT 1 WHERE EXISTS ....

Note: You can use the AsResultSet(IEnumerable) method in order to access the delay load functions from interfaces which return IEnumerable

var wouldBeEnumerable = repository.Find(o=>o.StatusConceptKey == StatusKeys.Active); if(wouldBeEnumerable.Any()) // results in a SELECT * FROM x - which is slow { Console.Write("I just did a slow thing"); } var resultSet = wouldBeEnumerable.AsResultSet(); if(resultSet.Any()) // results in SELECT EXISTS FROM { Console.Write("I just did a faster thing"); }
Example
var resultSet = repository.Find(o => o.StatusConcept.Mnemonic == "ACTIVE").AsResultSet(); resultSet = resultSet.OrderByDescending(o => o.VersionSequenceId); // adds ORDER BY vrsn_seq_id DESC resultSet = resultSet.Skip(10); // add OFFSET 10 to the query resultSet = resultSet.Take(10); // add FETCH FIRST 10 ROWS ONLY to query foreach(var result in resultSet.Select(o => o.Key)) // places SELECT id to query { Console.WriteLine(result); // output is a UUID }
See Also