3

我是新手CRM (version 4.0),我正在尝试根据给定的年份(也存储在实体中)返回“yearid”指南。到目前为止,我得到了:

    public static Guid GetYearID(string yearName)
    {

        ICrmService service = CrmServiceFactory.GetCrmService();

        // Create the query object.
        QueryExpression query = new QueryExpression("year");
        ColumnSet columns = new ColumnSet();
        columns.AddColumn("yearid");
        query.ColumnSet = columns;       

        FilterExpression filter = new FilterExpression();

        filter.FilterOperator = LogicalOperator.And;
        filter.AddCondition(new ConditionExpression
        {
            AttributeName = "yearName",
            Operator = ConditionOperator.Equal,
            Values = new object[] { yearName}
        });

        query.Criteria = filter;

    } 

但我的问题是:


A)除此之外还有哪些代码可以实际存储 Guid?
B) 使用 QueryExpression 是最有效的方法吗?

4

1 回答 1

2

回复:B)使用 sql 可能更快(我不知道多少甚至是否),但我会认为在 99% 的情况下,QueryExpression 在性能方面是完全可以接受的。

回复:A)你已经很接近了,我已经完成了下面的代码。

public static Guid GetYearID(string yearName) 
{
    ICrmService service = CrmServiceFactory.GetCrmService(); 

    // Create the query object. 
    QueryExpression query = new QueryExpression("year"); 

    //No need to ask for the id, it is always returned
    //ColumnSet columns = new ColumnSet(); 
    //columns.AddColumn("yearid");
    //query.ColumnSet = columns;

    FilterExpression filter = new FilterExpression(); 

    filter.FilterOperator = LogicalOperator.And; 
    filter.AddCondition(new ConditionExpression 
    { 
        AttributeName = "yearName", 
        Operator = ConditionOperator.Equal, 
        Values = new object[] { yearName} 
    }); 

    query.Criteria = filter; 

    //We have to use a retrieve multiple here
    //In theory we could get multiple results but we will assume we only ever get one
    BusinessEntityCollection years = service.RetrieveMultiple(query);

    DynamicEntity year = (DynamicEntity)years.BusinessEntities.First();

    return ((Guid)year["yearid"]);
}  
于 2012-10-18T12:26:29.957 回答