Use projection to optimize query performance
Queries in OData will by default return the entire entity (all properties of the entities are returned in the payload from the server) unless the $select OData query parameter is included.
Returning the entire set of properties can affect query performance and if possible, you can reduce the amount of properties returned in order to improve performance and reduce processing time.
To optimize the query performance you should always include a projection so the server only returns the specific properties that you need.
Protocol
On the protocol level this is accomplished by including a $select query option in the URL.
Example:
/OData/Files?$select=ID,Title,Created
This example will only return values for the ID, Title and Created properties of the Files (cases) entity.
.NET with WCF Data Services Client
On .net with WCF Data Services Client you can use AddQueryOption(“$select”, “ID,Title,Created”) or implicitly select the return properties by performing a projection of the results. See example below
Example:
// The resulting query: OData/Files?$select=ID,Title,Created
var query = from f in context.Files
select new File
{
ID = f.ID,
Title = f.Title,
Created = f.Created
};
Projections can also be performed on related expanded entities.
If you want to select Title and LetterDate from Records (metadata for documents), but also include the FileNo and Title of the File (case) together with the CustomLabel (role) of the Record Parties and their Name1, you can accomplish this by projection as shown below.
Example:
// The resulting query: /OData/Records()?$expand=File,Parties,Parties/Name&$select=Title,LetterDate,File/FileNo,File/Title,Parties/CustomLabel_Summary,Parties/Name/Name1
var query = from r in context.Records
select new Record
{
Title = r.Title,
LetterDate = r.LetterDate,
File = r.File == null ? null : new File // As r.File can be null in certain cases we need the null propagation check
{
FileNo = r.File.FileNo,
Title = r.File.Title
},
Parties = new DataServiceCollection<RecordContact>(
from p in r.Parties
select new RecordContact
{
CustomLabel_Summary = p.CustomLabel_Summary,
Name = new Contact
{
Name1 = p.Name.Name1
}
})
};