什么是实体引用和查询表达式?请举一些简单的例子

What is entity reference and Query Expression ? Please give some simple examples

什么是 EntityReference 和 QueryExpression?请给我一些简单的例子。

EntityReference

It is used for lookup fields in 365 – e.g. to link records via a 1 to many relationship. The lookup field is shown on the ‘child’. You need to specify the ‘parent’ entity type and record id.

For example 如果您正在创建帐户并想设置主要联系人。

Entity account = new Entity("account"); 
account["name"] = "James Account"; 
account["primarycontactid"] = new EntityReference("contact", contactId);
service.Create(account);

QueryExpression

QueryExpression provides an object model to construct a query. Queries can also be created using FetchXML, a proprietary XML based query language.

例如,您想要检索所有联系人的全名和电话号码。

QueryExpression query = new QueryExpression()
{
    Distinct = false,
    EntityName = Contact.EntityLogicalName,
    ColumnSet = new ColumnSet("fullname", "address1_telephone1"),
};
DataCollection<Entity> entityCollection = _service.RetrieveMultiple(query).Entities;