在CRM 4.0中检索单个Guid

时间:2012-10-18 10:48:34

标签: c# dynamics-crm dynamics-crm-4

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

    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是最有效的方法吗?

1 个答案:

答案 0 :(得分:2)

Re: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"]);
}