Dela via


QueryExpression Class

Definition

Contains a complex query expressed in a hierarchy of expressions.

public ref class QueryExpression sealed : Microsoft::Xrm::Sdk::Query::QueryBase
[System.Runtime.Serialization.DataContract(Name="QueryExpression", Namespace="http://schemas.microsoft.com/xrm/2011/Contracts")]
public sealed class QueryExpression : Microsoft.Xrm.Sdk.Query.QueryBase
[<System.Runtime.Serialization.DataContract(Name="QueryExpression", Namespace="http://schemas.microsoft.com/xrm/2011/Contracts")>]
type QueryExpression = class
    inherit QueryBase
Public NotInheritable Class QueryExpression
Inherits QueryBase
Inheritance
QueryExpression
Attributes

Examples

The following example shows how to use this message. For this sample to work correctly, you must be connected to the server to get an IOrganizationService interface instance.

static void QueryExpressionExample(IOrganizationService service) {
// 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()
{
      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(
                        attributeName: "name",
                        conditionOperator: ConditionOperator.Equal,
                        value: "Litware, Inc.")
                  }
            }
         }
      },
      Criteria =
      {
         Filters =
         {
            new FilterExpression
            {
                  FilterOperator = LogicalOperator.And,
                  Conditions =
                  {
                     new ConditionExpression(
                        attributeName: "address1_stateorprovince",
                        conditionOperator: ConditionOperator.Equal,
                        value: "WA"),
                     new ConditionExpression(
                        attributeName:"address1_city",
                        conditionOperator: ConditionOperator.In,
                        values: new string[] {
                              "Redmond",
                              "Bellevue" ,
                              "Kirkland",
                              "Seattle"}),
                     new ConditionExpression(
                        attributeName:"createdon",
                        conditionOperator:ConditionOperator.LastXDays,
                        value:30),
                     new ConditionExpression(
                        attributeName:"emailaddress1",
                        conditionOperator:ConditionOperator.NotNull)
                  },
            },
            new FilterExpression
            {
                  FilterOperator = LogicalOperator.Or,
                  Conditions =
                  {
                     new ConditionExpression(
                        attributeName:"address1_telephone1",
                        conditionOperator:ConditionOperator.Like,
                        value:"(206)%"),
                     new ConditionExpression(
                        attributeName:"address1_telephone1",
                        conditionOperator:ConditionOperator.Like,
                        value:"(425)%")
                  }
            }
         }
      }
};

EntityCollection entityCollection = service.RetrieveMultiple(query);

// Display the results.
Console.WriteLine("List all contacts matching specified parameters");
Console.WriteLine("===============================================");
foreach (Contact contact in entityCollection.Entities)
{
      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();
}         

Remarks

QueryExpression provides an object model to construct a query. Learn to build queries with QueryExpression

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.

Constructors

QueryExpression()

Initializes a new instance of the QueryExpression class.

QueryExpression(String)

Initializes a new instance of the QueryExpression class setting the EntityName property.

Fields

Empty

Properties

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.

DataSource

Gets or sets the source of data for the query.

DeserializationDepthLimit

For internal use only.

Distinct

Gets or sets whether the results of the query should only contain unique values.

EntityName

Gets or sets the logical name of the primary table for the query.

ExtensionData

Gets or sets the structure that contains extra data.

(Inherited from QueryBase)
ForceSeek

For internal use only.

LinkEntities

Gets a collection of the links between multiple tables.

NoLock

Setting this property is not necessary.

Orders

Gets the order in which the records are returned from the query.

PageInfo

Gets or sets the number of pages and the number of records per page to return from the query.

QueryHints

Gets or sets a hint for generated SQL which affects the query's execution.

SerializationDepthLimit

For internal use only.

SubQueryExpression

For internal use only.

TopCount

Gets or sets the number of rows to be returned.

Methods

Accept(IQueryExpressionVisitor)
AddLink(String, String, String, JoinOperator)

Adds the specified link to the query expression setting the LinkToEntityName, LinkFromAttributeName, LinkToAttributeName, and JoinOperator properties.

AddLink(String, String, String)

Adds the specified link to the query expression setting the LinkToEntityName, LinkFromAttributeName and LinkToAttributeName properties.

AddOrder(String, OrderType, String, String)

Adds the specified OrderExpression to the query expression, setting the AttributeName and OrderType, Alias and EntityName properties.

AddOrder(String, OrderType)

Adds the specified OrderExpression to the query expression, setting the AttributeName and OrderType properties.

InternalResetSerializationCounters()

For internal use only.

Applies to