QueryExpression 类

应用到: CRM 2015 on-prem, CRM Online

Contains a complex query expressed in a hierarchy of expressions.

命名空间: Microsoft.Xrm.Sdk.Query
程序集: Microsoft.Xrm.Sdk (在 Microsoft.Xrm.Sdk.dll 中)

语法

声明
<DataContractAttribute(Name:="QueryExpression", Namespace:="https://schemas.microsoft.com/xrm/2011/Contracts")> _
Public NotInheritable Class QueryExpression
    Inherits QueryBase
[DataContractAttribute(Name="QueryExpression", Namespace="https://schemas.microsoft.com/xrm/2011/Contracts")] 
public sealed class QueryExpression : QueryBase

示例

// 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
//               )

String fetchXml = @"<fetch mapping=""logical"" count=""50"" version=""1.0"">
                        <entity name=""contact"">
                            <attribute name=""address1_telephone1"" />
                            <attribute name=""contactid"" />
                            <attribute name=""firstname"" />
                            <attribute name=""lastname"" />
                            <filter>
                                <condition attribute=""address1_stateorprovince"" operator=""eq"" value=""WA"" />
                                <condition attribute=""address1_city"" operator=""in"">
                                    <value>Redmond</value>
                                    <value>Bellevue</value>
                                    <value>Kirkland</value>
                                    <value>Seattle</value>
                                </condition>
                                <condition attribute=""createdon"" operator=""last-x-days"" value=""30"" />
                                <condition attribute=""emailaddress1"" operator=""not-null"" />
                                <filter type=""or"">
                                    <condition attribute=""address1_telephone1"" operator=""like"" value=""(206)%"" />
                                    <condition attribute=""address1_telephone1"" operator=""like"" value=""(425)%"" />
                                </filter>
                            </filter>
                            <link-entity name=""account"" from=""accountid"" to=""parentcustomerid"">
                                <filter>
                                    <condition attribute=""name"" operator=""eq"" value=""Litware, Inc."" />
                                </filter>
                            </link-entity>
                        </entity>
                    </fetch>";

// Build fetch request and obtain results.
RetrieveMultipleRequest efr = new RetrieveMultipleRequest()
{
    Query = new FetchExpression(fetchXml)
};

EntityCollection entityResults = ((RetrieveMultipleResponse)_service.Execute(efr)).EntityCollection;
 

// Display the results.
Console.WriteLine("List all contacts matching specified parameters");
Console.WriteLine("===============================================");
foreach (var e in entityResults.Entities)
{
    Console.WriteLine("Contact ID: {0}", e.Id);
}


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
//               )

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();

备注

A query expression can be expressed as FetchXML or as a hierarchy of expressions.

继承层次结构

System.Object
   Microsoft.Xrm.Sdk.Query.QueryBase
    Microsoft.Xrm.Sdk.Query.QueryExpression

线程安全性

此类型的所有公共静态(Visual Basic 中的 Shared)成员都是线程安全成员。不保证任何实例成员是线程安全成员。

平台

开发平台

Windows Vista、Windows Server 2003 和

目标平台

Windows Vista,Windows XP

Change History

另请参阅

参考

QueryExpression 成员
Microsoft.Xrm.Sdk.Query 命名空间

其他资源

Build Queries with QueryExpression
Sample: Retrieve Multiple with Query Expression
Sample: Query Connection Roles by Entity Type Code (Early Bound)

Send comments about this topic to Microsoft.
© 2014 Microsoft Corporation. All rights reserved.