QueryExpression Class
Applies To: Dynamics CRM 2015
Contains a complex query expressed in a hierarchy of expressions.
Namespace: Microsoft.Xrm.Sdk.Query
Assembly: Microsoft.Xrm.Sdk (in Microsoft.Xrm.Sdk.dll)
Inheritance Hierarchy
System.Object
Microsoft.Xrm.Sdk.Query.QueryBase
Microsoft.Xrm.Sdk.Query.QueryExpression
Syntax
[DataContractAttribute(Name = "QueryExpression", Namespace = "https://schemas.microsoft.com/xrm/2011/Contracts")]
public sealed class QueryExpression : QueryBase
[DataContractAttribute(Name = "QueryExpression", Namespace = "https://schemas.microsoft.com/xrm/2011/Contracts")]
public ref class QueryExpression sealed : QueryBase
[<Sealed>]
[<DataContractAttribute(Name = "QueryExpression", Namespace = "https://schemas.microsoft.com/xrm/2011/Contracts")>]
type QueryExpression =
class
inherit QueryBase
end
<DataContractAttribute(Name := "QueryExpression", Namespace := "https://schemas.microsoft.com/xrm/2011/Contracts")>
Public NotInheritable Class QueryExpression
Inherits QueryBase
Constructors
Name | Description | |
---|---|---|
QueryExpression() | Initializes a new instance of the QueryExpression class. |
|
QueryExpression(String) | Initializes a new instance of the QueryExpression class setting the entity name. |
Properties
Name | Description | |
---|---|---|
ColumnSet | Gets or sets the columns to include. |
|
Criteria | Gets or sets the complex condition and logical filter expressions that filter the results of the query. |
|
Distinct | Gets or sets whether the results of the query contain duplicate entity instances. |
|
EntityName | Gets or sets the logical name of the entity. |
|
ExtensionData | Gets or sets the structure that contains extra data.(Inherited from QueryBase.) |
|
LinkEntities | Gets a collection of the links between multiple entity types. |
|
NoLock | Gets or sets a value that indicates that no shared locks are issued against the data that would prohibit other transactions from modifying the data in the records returned from the query. |
|
Orders | Gets the order in which the entity instances are returned from the query. |
|
PageInfo | Gets or sets the number of pages and the number of entity instances per page returned from the query. |
|
TopCount | Gets or sets the number of rows to be returned. |
Methods
Name | Description | |
---|---|---|
AddLink(String, String, String) | Adds the specified link to the query expression setting the entity name to link to, the attribute name to link from and the attribute name to link to. |
|
AddLink(String, String, String, JoinOperator) | Adds the specified link to the query expression setting the entity name to link to, the attribute name to link from and the attribute name to link to. |
|
AddOrder(String, OrderType) | Adds the specified order expression to the query expression. |
|
Equals(Object) | (Inherited from Object.) |
|
GetHashCode() | (Inherited from Object.) |
|
GetType() | (Inherited from Object.) |
|
ToString() | (Inherited from Object.) |
Remarks
QueryExpression provides an object model to construct a query. Queries can also be created using FetchXML, a proprietary XML based query language. You can convert queries between FetchXML and QueryExpression using FetchXmlToQueryExpressionRequest and QueryExpressionToFetchXmlRequest messages. More information: Sample: Convert queries between Fetch and QueryExpression.
Examples
// Build the following SQL query using QueryExpression:
//
// SELECT contact.fullname, contact.address1_telephone1
// FROM contact
// LEFT OUTER JOIN account
// ON contact.parentcustomerid = account.accountid
// AND
// account.name = 'Litware, Inc.'
// WHERE (contact.address1_stateorprovince = 'WA'
// AND
// contact.address1_city in ('Redmond', 'Bellevue', 'Kirkland', 'Seattle')
// AND
// contact.address1_telephone1 like '(206)%'
// OR
// contact.address1_telephone1 like '(425)%'
// AND
// DATEDIFF(DAY, contact.createdon, GETDATE()) > 0
// AND
// DATEDIFF(DAY, contact.createdon, GETDATE()) < 30
// AND
// contact.emailaddress1 Not NULL
// )
QueryExpression query = new QueryExpression()
{
Distinct = false,
EntityName = Contact.EntityLogicalName,
ColumnSet = new ColumnSet("fullname", "address1_telephone1"),
LinkEntities =
{
new LinkEntity
{
JoinOperator = JoinOperator.LeftOuter,
LinkFromAttributeName = "parentcustomerid",
LinkFromEntityName = Contact.EntityLogicalName,
LinkToAttributeName = "accountid",
LinkToEntityName = Account.EntityLogicalName,
LinkCriteria =
{
Conditions =
{
new ConditionExpression("name", ConditionOperator.Equal, "Litware, Inc.")
}
}
}
},
Criteria =
{
Filters =
{
new FilterExpression
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression("address1_stateorprovince", ConditionOperator.Equal, "WA"),
new ConditionExpression("address1_city", ConditionOperator.In, new String[] {"Redmond", "Bellevue" , "Kirkland", "Seattle"}),
new ConditionExpression("createdon", ConditionOperator.LastXDays, 30),
new ConditionExpression("emailaddress1", ConditionOperator.NotNull)
},
},
new FilterExpression
{
FilterOperator = LogicalOperator.Or,
Conditions =
{
new ConditionExpression("address1_telephone1", ConditionOperator.Like, "(206)%"),
new ConditionExpression("address1_telephone1", ConditionOperator.Like, "(425)%")
}
}
}
}
};
DataCollection<Entity> entityCollection = _service.RetrieveMultiple(query).Entities;
// Display the results.
Console.WriteLine("List all contacts matching specified parameters");
Console.WriteLine("===============================================");
foreach (Contact contact in entityCollection)
{
Console.WriteLine("Contact ID: {0}", contact.Id);
Console.WriteLine("Contact Name: {0}", contact.FullName);
Console.WriteLine("Contact Phone: {0}", contact.Address1_Telephone1);
}
Console.WriteLine("<End of Listing>");
Console.WriteLine();
' Build the following SQL query using QueryExpression:
'
' SELECT contact.fullname, contact.address1_telephone1
' FROM contact
' LEFT OUTER JOIN account
' ON contact.parentcustomerid = account.accountid
' AND
' account.name = 'Litware, Inc.'
' WHERE (contact.address1_stateorprovince = 'WA'
' AND
' contact.address1_city in ('Redmond', 'Bellevue', 'Kirkland', 'Seattle')
' AND
' contact.address1_telephone1 like '(206)%'
' OR
' contact.address1_telephone1 like '(425)%'
' AND
' DATEDIFF(DAY, contact.createdon, GETDATE()) > 0
' AND
' DATEDIFF(DAY, contact.createdon, GETDATE()) < 30
' AND
' contact.emailaddress1 Not NULL
' )
Dim query As New QueryExpression() With
{
.Distinct = False,
.EntityName = Contact.EntityLogicalName,
.ColumnSet = New ColumnSet("fullname", "address1_telephone1")
}
Dim queryLink As New LinkEntity With
{
.LinkToEntityName = Account.EntityLogicalName,
.LinkFromAttributeName = "parentcustomerid",
.LinkToAttributeName = "accountid",
.JoinOperator = JoinOperator.LeftOuter
}
queryLink.LinkCriteria().AddCondition("name",
ConditionOperator.Equal,
"Litware, Inc.")
query.LinkEntities().Add(queryLink)
Dim queryCriteraFilter1 As New FilterExpression()
queryCriteraFilter1.FilterOperator = LogicalOperator.And
queryCriteraFilter1.AddCondition(
New ConditionExpression("address1_stateorprovince",
ConditionOperator.Equal, "WA"))
queryCriteraFilter1.AddCondition(
New ConditionExpression("address1_city",
ConditionOperator.In,
New String() {"Redmond",
"Bellevue",
"Kirkland",
"Seattle"}))
queryCriteraFilter1.AddCondition(
New ConditionExpression("createdon",
ConditionOperator.LastXDays,
30))
queryCriteraFilter1.AddCondition(
New ConditionExpression("emailaddress1",
ConditionOperator.NotNull))
Dim queryCriteraFilter2 As New FilterExpression()
queryCriteraFilter2.FilterOperator = LogicalOperator.Or
queryCriteraFilter2.AddCondition(
New ConditionExpression("address1_telephone1",
ConditionOperator.Like,
"(206)%"))
queryCriteraFilter2.AddCondition(
New ConditionExpression("address1_telephone1",
ConditionOperator.Like,
"(425)%"))
query.Criteria.Filters.AddRange(queryCriteraFilter1,
queryCriteraFilter2)
Dim entityCollection As DataCollection(Of Entity) =
_service.RetrieveMultiple(query).Entities
' Display the results.
Console.WriteLine("List all contacts matching specified parameters")
Console.WriteLine("===============================================")
For Each contact As Contact In entityCollection
Console.WriteLine("Contact ID: {0}", contact.Id)
Console.WriteLine("Contact Name: {0}", contact.FullName)
Console.WriteLine("Contact Phone: {0}", contact.Address1_Telephone1)
Next contact
Console.WriteLine("<End of Listing>")
Console.WriteLine()
Thread Safety
Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
See Also
Microsoft.Xrm.Sdk.Query Namespace
Build queries with QueryExpression
Sample: Retrieve multiple with the QueryExpression class
Sample: Query connection roles by entity type code (early bound)
Return to top
© 2016 Microsoft. All rights reserved. Copyright